雄辩的创建说movie_id即使存在也不存在

时间:2019-04-12 15:46:27

标签: laravel eloquent

所以基本上,我试图使用Eloquent创建方法在数据库中创建新行,但是它不能正常工作。

这是我控制器中的代码。

$movie->review(request()->all() + ['user_id' => auth()->id()]);

这是我的电影模特

class Movie extends Model
{
    protected $fillable = [
        'id', 'title', 'poster_path'
    ];

    public function reviews()
    {
        return $this->hasMany(Review::class);
    }

    public function review($review)
    {
        $this->reviews()->create($review);
    }
}

是的,我已经将movie_id放入了Review模型中。

protected $fillable = [
    'movie_id', 'user_id', 'heading', 'rating', 'comment'
];

我检查了我的表单是否正确发送了所有数据,并且确实发送了。

  "_token" => "..."
  "movie_id" => "5"
  "rating" => "7"
  "heading" => "Test heading"
  "comment" => "Test comment"
  "user_id" => 1

当我检查数据库时,所有内容都在movie_id旁边设置。它为Null,我不知道为什么。

0 个答案:

没有答案
相关问题