Categories: php, laravel, eloquent

how to access related table in laravel

1 answer

I have some related models. A user can belong to many groups and groups can have any number of siteCensuses. I want to run a query but limit the results to only those siteCensus that he is allowed to see. In order to do that, I have to know if a given id is in the list of a user's siteCensuses. The models and tables are named predictably.

I've tried to set a relation like this:

$user = User::first(); $user->hasManyThrough('App\SiteCensus', 'App\GroupSiteCensus', 'group_id','id',,'sites_census_id') 

but no luck. Another thing I tried is this:

$user->load('groups.siteCensus')

which does get me a list of all the siteCensuses, but I can't easily extract just the id fields to check for membership.

Ultimately, I need to plug it into the model like this:

Class Patient extends Model {     protected static function boot()     {         parent::boot();         static::addGlobalScope('status', function (Builder $builder) {                 $site_census_ids = .......                 $builder->where(function (Builder $q) use ($site_census_ids) {                     return $q->whereIn('sites_census_id', $site_census_ids);                 });             });     } } 

Some relevant code is below:

class Group extends Model {     public function users()     {         return $this->belongsToMany("App\User");     }      public function siteCensus()     {         return $this->belongsToMany("App\SiteCensus", 'group_site_census', 'group_id', 'sites_census_id');     }  } 
class User extends Model {     public function groups()     {         return $this->belongsToMany('App\Group');     } } 
class SiteCensus extends Model {     public function groups()     {         return $this->belongsToMany("App\Group");     } } 

All answers to this question, which has the identifier 61222463

The best answer:

if i understand you correctly .. you want $site_census_ids that the user have simple enough:

$site_census_ids=$myUser->groups() ->join('group_site_census','group_site_census.group_id','=','groups.id') ->join('sites_census','sites_census.id','group_site_census.sites_census_id') ->select('sites_census.id')->get()->pluck('id'); 

please make sure the table names 'sites_census','groups' are correct

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data