/ / Relazione Many to Many in laravel 4 - laravel

Molte a molte relazioni in laravel 4 - laravel

Ho una tabella di film e una tabella di generi e ho dichiarato le mie relazioni come mostrato di seguito

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

e nel modello di genere

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

e questo è come appare il mio controller, ma quando provo a caricare film, il film viene caricato correttamente ma restituisce un errore mostrato di seguito

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

E questo è l'errore

Argomento 1 superatoper IlluminateDatabaseEloquentRelationsBelongsToMany :: formatSyncList () deve essere del tipo array, stringa fornita, richiamata E: CinemavendorlaravelframeworksrcIlluminateDatabaseEloquentRelationsBelongsToMany.php sulla linea 599 e definita

Chiunque possa darmi una mano ti sarò grato grazie.

risposte:

0 per risposta № 1

sync() si aspetta un array, quindi puoi provare:

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

Se la tua versione di PHP è 5.4 o successiva, puoi usare la sintassi dell'array breve così

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