I currently have a resources table :
and a resource_user_role_restrictions table
My model contains the following function that checks the resource_user_role_restrictions table and returns all resources whose resource_user_role_restrictions.role_id matches the passed in $userRoleId and if it's viewable column set to 1:
public function scopeResourcesFilter($query, array $filters) {
$userRoleId = $filters[0];
$query->whereHas('resourceUserRoleRestrictions', function ($query) use($userRoleId){
$query->where('viewable', 1)->where("role_id", $userRoleId );
});
}
This function is called in my controller:
$userRoleId = Auth::user()->role_id;
$resources = Resource::resourcesFilter([$userRoleId])->with('country', 'resourceUserRoleRestrictions')->get();
However, this now needs to be converted into a black list whereby the viewable column is removed and any resource on the table is assumed to be restricted and not viewable, thus not returned. I cant figure out how to get it woking. Ideally this function should now return all items from the resources table except those whose resource_user_role_restrictions.user_role matches the passed in $userRoleId.
Any help at all would be greatly appreciated.
looks like whereDoesntHave()
is what I needed.
$query->whereDoesntHave('resourceUserRoleRestrictions', function ($query) use($userRoleId){
$query->where("role_id", $userRoleId );
});