laravel 5 advance eloquent multiple selection -


i have table containing multiple artist. while creating event (updating event table) need select artists want perform @ event , need add ids in event_artist (transitive) table (that contains event_id & artist_id). how shall proceed?

you define many many relationship.

in artist model class:

public function events() {     $this->belongstomany(event::class); } 

in event model class:

public function artists() {     $this->belongstomany(artist::class); } 

this assumes pivot table named artist_event (the other 2 table names combined in alphabetical order). otherwise can pass pivot table name second argument belongstomany: belongstomany(event::class, 'event_artist').

then can attach artist event using $event->artists()->attach($artist);. or detach $event->artists()->detach($artist);. can sync collection of artists: $event->artists()->sync($artists);. can attach, detach or sync events artists. 3 methods accept either eloquent models or ids.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -