Butochnikov
1/26/2015 - 7:16 PM

PolymorphSEO

PolymorphSEO

Простая мысль: отделить сео от материала - дабы не изобретать каждый раз велосипед. Новости, статьи, категории ,посты - все с единой структурой сео-данных.

<?php

use Path\To\NameSpace\SeoTrait;

class AnyModel extends Eloquent {

    use SeoTrait;

}

и сео-данные прицеплены к модели.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSeoTable extends Migration {

	public function up()
	{
		Schema::create('seo', function(Blueprint $table)
		{
			$table->increments('id');
			$table->string('title', 70)->nullable();
			$table->string('h1', 100)->nullable();
			$table->string('description', 156)->nullable();
			$table->string('keywords', 200)->nullable();
			$table->morphs('material');
		});
	}

	public function down()
	{
		Schema::dropIfExists('seo');
	}

}
<?php 

use Path\To\NameSpace\Seo;

trait SeoTrait {

	public function seo()
	{
		return $this->morphOne(Seo::class, 'material');
	}
	
	public function scopeWithSeo($query)
	{
		return $query->with('seo');
	}

}
<?php

use Illuminate\Database\Eloquent\Model;

class Seo extends Model {
  
	protected $table = 'seo';
  
	public function material()
	{
		return $this->morphTo();
	}

}