laravel 4中的多对多关系

时间:2016-03-04 13:49:00

标签: laravel

我有一个电影桌和一个流派表,我说明了我的关系,如下所示

public function genres()
{
    return $this->belongsToMany('Genre');
}

和流派模型

public function movies()
{
    return $this->belongsToMany('Movie');
}

这就是我的控制器的样子,但当我尝试上传电影时,电影上传成功,但它返回如下所示的错误

$movie = new Movie();
$movie->status_id = Input::get('status');
$movie->title = Input::get('title');
$movie->summary = Input::get('summary');
$movie->duration = Input::get('duration');
$movie->cast = Input::get('cast_list');
$movie->director = Input::get('director');
$movie->price = Input::get('movie_price');
$movie->dimension = Input::get('dimension');
$movie->time_showing = Input::get('time_showing');
$movie->time_showing1 = Input::get('time_showing1');
$movie->photo = $photoname;
$movie->date_showing = Input::get('date_showing');
$movie->save();
$movie->genres()->sync(Input::get('genre'));
return Redirect::back()->with('success', 'movie added successfully');

这就是错误

  

参数1传递给   照亮\数据库\雄辩\关系\ BelongsToMany :: formatSyncList()   必须是类型数组,给定的字符串,调用   E:\电影\供应商\ laravel \框架的\ src \照亮\数据库\雄辩\关系\ BelongsToMany.php   在第599行并定义

任何能够帮助我的人都将不胜感激。

1 个答案:

答案 0 :(得分:0)

sync()需要array,因此您可以尝试:

$movie->genres()->sync(array(Input::get('genre')));

如果你的PHP版本是5.4或更高版本,你可以使用短数组语法

$movie->genres()->sync([Input::get('genre')]);
相关问题