mardi 12 septembre 2017

How to return all models in BelongsToMany relationship

I have relationship on User model like 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');
    }

    // For admin role I want to return all brands, from Brand Model
    // ?? 
}

For Admin role I want to return all rows from Brand model, How can I get that? And that should be instance of BelongsToMany class, then only it won't break code in my controller.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire