我正在玩弄Laravel中的多態關係。當我嘗試喜歡的東西,我得到一個錯誤:Laravel:多態關係 - 整合約束違規:1048'likeable_id'列不能爲空
Integrity constraint violation: 1048 Column 'likeable_id' cannot be null - Error Image
如果我添加likeable_id爲null,則likeable_id是空的:SQL DB
像模式
public function likeable()
{
return $this->morphTo();
}
郵政型號
public function likes()
{
return $this->morphMany(Like::class, 'likeable');
}
PostLikeController
public function store(Request $request, Place $topic, Post $post)
{
$like = new Like;
$like->user()->associate($request->user());
$post->likes()->save($like);
return redirect()->route('web.consumer.post.index');
}
遷移
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
$table->integer('likeable_id')->unsigned();
$table->string('likeable_type');
$table->integer('user_id')->unsigned()->index();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
如果我手動輸入'id'。像count()方法那麼完美。 –