1
votes

I have a movies table and a genres table and i stated my relationships as shown below

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

and in Genre model

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

and this is how my controller looks like but when i try to upload movies, the movie is uploaded successfully but it returns an error shown below

$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');

And this is the error

Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::formatSyncList() must be of the type array, string given, called in E:\Cinema\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\BelongsToMany.php on line 599 and defined

Anyone who can give me a hand i will be grateful thank you.

1

1 Answers

0
votes

sync() expects an array, so you could try:

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

If your PHP version is 5.4 or higher, you can use short array syntax so

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