php - Including another set of results into many-to-many relationship -
let's have 3 tables:
- article: id, ...
- advert: id, display_on_every_subsite, ...
- 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
Post a Comment