php - Distant HasManyThrough -


i have 4 models:

  1. user
  2. client
  3. store
  4. opportunity

the relationships defined such:

  • user hasmany client
  • client hasmany store
  • store hasmany opportunity
  • user hasmanythrough store, client (this works)

the problem i'm attempting access user->opportunity relationship via built-in laravel relationships, doesn't seem if can without custom query or additional user_id column on opportunities table allow direct access (even though 1 can inferred store->client relationship). i'm not fan of nested foreach loops if can avoided.

my question:

is there way go 1 level deeper , directly access user's opportunities in scenario? actual model code , relevant relationships follows:

user

class user extends eloquent{     public function clients(){         return $this->hasmany('client');     }     public function stores(){         return $this->hasmanythrough('store', 'client');     }     public function proposals(){         return $this->hasmany('proposal');     }     public function opportunities(){ //this job, feel better         return opportunity::join('stores', 'stores.id', '=', 'opportunities.store_id')->                             join('clients', 'clients.id', '=', 'stores.client_id')->                             join('users', 'users.id', '=', 'clients.user_id')->                             select('opportunities.*')->                             where('users.id', $this->id);     }     public function getopportunitiesattribute(){ //this helps mimic hasmanythrough shorthand         return $this->opportunities()->get();            } } 

client

class client extends eloquent{     public function stores(){         return $this->hasmany('store');     }     public function user(){         return $this->belongsto('user');     }     public function opportunities(){         return $this->hasmanythrough('opportunity', 'store');     } } 

store

class store extends eloquent {     public function client(){         return $this->belongsto('client');     }     public function opportunities(){         return $this->hasmany('opportunity');     } } 

opportunity

class opportunity extends eloquent {     public function store(){         return $this->belongsto('store');     } } 

i don't think there such method in laravel. have create custom query. custom query can expensive since multiple queries performed. thus, optimum solution this, according me, relate user , opportunity foreign key.

however, if don't desire link user , opportunity foreign key, can create custom query handle this. add "hasmanythrough" relation between opportunity , client model like,

    <?php     class client extends eloquent{         public function store(){             return $this->hasmany('store');         }         public function user(){             return $this->belongsto('user');         }          public function opportunity(){             return $this->hasmanythrough('opportunity', 'store');         }     } 

then create static function in user model.

    <?php      class user extends eloquent implements userinterface, remindableinterface {          use usertrait, remindabletrait;          public function client(){             return $this->hasmany('client');         }         public function store(){             return $this->hasmanythrough('store', 'client');         }          public static function getopportunityofuser($userid)         {              $clients = user::find($userid)->client;              foreach ($clients $client) {                 $opportunities[] = client::find($client->id)->opportunity;             }              return $opportunities;         }     } 

now can access opportunity realted user in 1 go like,

    route::get('/', function()     {             return $usersopportunities = user::getopportunityofuser(1);     }); 

this return opportunity of clients related user id '1'.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -