Laravel morphMany Returning Null id
I’m trying to create Favorites for Replies, I have these models
AppReply
public function favorites()
{
return $this->morphMany('AppFavorite', 'favorited');
}
AppFavorite
public function favorited()
{
return $this->morphTo();
}
Here is my DB table
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('favorited_id')->unsigned();
$table->string('favorited_type', 50);
$table->timestamps();
$table->unique(['user_id', 'favorited_id', 'favorited_type']);
});
}
When I try to create a new favorite like this:
public function store(Reply $reply){
Favorite::create([
'user_id' => Auth::user()->id,
'favorited_id' => $reply->id,
'favorited_type' => get_class($reply)
]);
return back();
}
I get the error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'favorited_id' cannot be null (SQL: insert into `favorites` (`user_id`, `favorited_id`, `favorited_type`, `updated_at`, `created_at`) values (4, , AppReply, 2018-03-06 04:27:11, 2018-03-06 04:27:11))
Someone please help me here.
from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-morphmany-returning-null-id/
via Lzo Media
No comments:
Post a Comment