taggable
そのため、複数のタグを関連付けることができるテーブルを作成しようとしていますが、Laravel のポリモーフィックな関係models
が最適であると感じました。
残念ながら、次のセットアップではそれらを機能させることができないようです。を実行すると、次のエラーが表示されますphp artisan migrate:refresh --seed
。
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class name must be a valid object or a string","file":"...\\vendor\\laravel\\framework\\src\\
Illuminate\\Database\\Eloquent\\Model.php","line":527}}
Taggable
この問題は、モデルのmorphTo
名前が以下に概説されているものと同じであることが原因であると考えています。これを変更すると、問題が修正されます。なぜこれが問題を引き起こすのですか?
タグ付け可能なモデル
class Taggable extends Eloquent {
protected $table = 'taggable';
public function taggable()
{
return $this->morphTo();
}
}
モデルを追跡する
class Track extends Eloquent {
protected $table = 'tracks';
protected $fillable = array('title', 'year', 'image');
protected $guarded = array('id');
public function playlists()
{
return $this->belongsToMany('Playlist');
}
public function tags()
{
return $this->morphMany('Taggable', 'taggable');
}
}
タグモデル
class Tag extends Eloquent {
protected $table = 'tags';
protected $fillable = array('title', 'description');
protected $guarded = array('id');
}
移行
Schema::create('taggable', function(Blueprint $table)
{
$table->increments('id');
$table->integer('taggable_id');
$table->string('taggable_type');
$table->integer('tag_id');
$table->timestamps();
});
DatabaseSeeder スニピット
...
DB::table('taggable')->delete();
$track1 = Track::find(1);
$idm = Tag::find(1);
$track1->tags()->create(array('tag_id' => $idm->id));
...
この問題について何か助けていただければ幸いです。