php - How to correctly setup the eloquent relationships for pivot tables in Laravel 5.2 -


i have 3 tables in db follows (not of columns minimised clarity):

admins (id, community_id, email, password, level) community (community_id, community_name) community_results (community_result_id, community_session_id) community_sessions (community_session_id, community_id) 

i have setup associated models , within admin controller have following code grabs results in database laravel collection (and works fine). admin table contains 2 types of admin user - 1 'superuser' has access every single row in db, , other type 'community admin' , have access community results (defined level column in admins table).

when logged admin 'community admin' want results community (admins.community_id foreign key in instance relates admin community).

e.g john doe 'community admin' 'acme community' , belongs community_id 5, when logged in 'community results' of 'community sessions' relate particular community (community_id foreign key in community_sessions table).

so i'd need find way create modified version of results() relation within communitysession.php model rather query every row retrieve of 1 or more community_session_id's - alternatively there way using community.php model create relation pull in results using relation following...

$community = community::find(5); $community->results; // pull results community using relation 

can suggest best way this? in advance

class adminresultcontroller extends controller {    public function index()    {       // if logged in 'superuser' admin results       if (auth::guard('admin')->user()->level == 1)        {          $results = communityresult::all();       } else {         // logged in 'standard' admin community results         $results = new communityresult;         // how collection of results community       }    } } 

// communityresult.php (model)

class communityresult extends model {   public $primarykey = 'community_result_id';    public function session()   {     return $this->hasone('app\communitysession', 'community_session_id', 'community_session_id');   } } 

// communitysession.php (model)

class communitysession extends model {   public $primarykey = 'community_session_id';    public function community()   {     return $this->belongsto('app\community', 'community_id');   }    public function results()   {     return $this->hasmany('app\communityresult', 'community_session_id');   }   } 

// community.php (model)

class community extends model {   public $table = 'community';   public $primarykey = 'community_id';    public function sessions()   {     return $this->hasmany('app\communitysession', 'community_id');   } } 

auth::guard('admin')->user()->with('sessions.results') 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -