Friday 15 August 2014

How do I use a polymorphic many-to-many relationship in Laravel 4.1? -



How do I use a polymorphic many-to-many relationship in Laravel 4.1? -

as illustration question, allow me utilize tv shows, movies, , actors. each tv show , film has multiple actors , every actor in 1 or more tv shows and/or movies. if set polymorphic many-to-many relationship between them seems can query actor of tv shows , of movies separately despite googling can come with, haven't found way query union of of actor's tv shows and/or movies in 1 collection. isn't usefulness of many-to-many polymorphic relationship?

if turns out indeed possible, add together notes including table , model setup accomplish because haven't been able working yet. if isn't possible, can help me understand goal of such relationship because don't understand it. seems me without query, don't gain on using 2 many-to-many relationships.

i'm not sure if solve question, should able both tv shows , movies in 1 collection using merge

example model demo

class actor { public function tv_shows() { homecoming ... } public function movies() { homecoming ... } }

you can combine them so:

actor::get(1)->movies->merge(actor::get(1)->tv_shows);

that homecoming single collection of movies , tv shows.

to create life easier create follow function in actor class

public function tvandmovies() { homecoming self::movies->merge(self::tv_shows); }

hopefully helps

laravel laravel-4 eloquent

No comments:

Post a Comment