Skip to content

Instantly share code, notes, and snippets.

@greabock
Last active December 1, 2018 21:49
Show Gist options
  • Save greabock/c0d226c887a3edb1d0f7 to your computer and use it in GitHub Desktop.
Save greabock/c0d226c887a3edb1d0f7 to your computer and use it in GitHub Desktop.
PolymorphSEO
<?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 Illuminate\Database\Eloquent\Model;
class Seo extends Model {
protected $table = 'seo';
public function material()
{
return $this->morphTo();
}
}
<?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 Path\To\NameSpace\SeoTrait;

class AnyModel extends Eloquent {

	use SeoTrait;

}

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

@qant
Copy link

qant commented Jan 30, 2015

Спасибо! Но думаю $table->string('keywords', 200)->nullable(); уже не актуально )

И кстати, в очень скором времени будет более важно указывать микроразметку или микроформаты, так что рекомендую на будущее заложить что-то на эту тему.

что то вроде $table->string('microformat'); и указать как минимум Articles или Profucts в идеале связанными с Reviews )

@greabock
Copy link
Author

@qant Можно про микроформаты поподробнее? Я что-то совсем не в теме.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment