I'm trying to display all campaigns belongs to a parent category
Category Model :
public function parents()
{
return $this->hasMany('App\Category');
}
public function categories()
{
return $this->belongsTo('App\Category','id','parent_id','categories');
}
public function campaigns()
{
return $this->belongsToMany('App\Campaign' );
}
Campaign Model :
public function categories()
{
return $this->belongsToMany('App\Category' , 'campaign_category', 'campaign_id', 'category_id');
}
Sorry, i just did this :D
$campaigns = Campaign::whereHas('categories', function($q) use($id) {
$q->where('parent_id', '=', $id);
})->get();