php - How to return all models in BelongsToMany relationship -


i have relationship on user model below:

public function brands() {     $roles = config('constants.roles');      if ($this->hasrole($roles['brand_site_admin'])) {         return $this->belongstomany(brand::class, 'brand_has_users');     } else      if ($this->hasrole($roles['client_admin'])) {         return $this->belongstomany(brand::class, 'brand_has_client_admin');     }      // admin role want return brands, brand model     // ??  } 

for admin role want return rows brand model, how can that? , should instance of belongstomany class, won't break code in controller.

update:

when $user->brands() want brands brands table if $user admin (in above code if doesn't goes in condition it's admin).

i think should try suggested in this post, first create relationships

    $roles = config('constants.roles'); public function siteadminbrands()     {         return $this->hasmany(brand::class, 'brand_has_users');     }          public function clientadminbrands()     {         return $this->hasmany(brand::class, 'brand_has_client_admin');     }      public function brands($query)     {         return $query               ->when($this->hasrole($roles['brand_site_admin']),function($q){                   return $q->with('siteadminbrands');              })              ->when($this->hasrole($roles['client_admin']),function($q){                   return $q->with('clientadminbrands');              });      } } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -