php - Including another set of results into many-to-many relationship -


let's have 3 tables:

  1. article: id, ...
  2. advert: id, display_on_every_subsite, ...
  3. article_advert: advert_id, article_id

i have eloquent's relationship: belongstomany between article , advert - article_advert pivot table.

the problem need fetch adverts specified article(s) , adverts display_on_every_subsite = 1.

i'm trying achieve using unions, i've @ moment:

$this->belongstomany('advert', 'article_advert', 'article_id', 'advert_id')->union(advert::allsubpages()->selectraw('`advert`.*, `advert`.`id` `pivot_advert_id`, null `pivot_article_id`')->getquery()); 

the problem when pivot_article_id null, eloquent not attach fetched rows related model.

it's that, change following:

// assume have inside article model  $articleid = $this->id;  $this->belongstomany('advert', 'article_advert', 'article_id', 'advert_id')     ->union(advert::allsubpages()         ->selectraw("`advert`.*, `advert`.`id` `pivot_advert_id`, '$articleid' `pivot_article_id`")         ->where('display_on_every_subsite','=','1')         ->getquery()); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -