Querying two many to many relationships - laravel

I have a users table:
id | name
I have a role table:
id | role
I have a user_role pivot table:
user_id | role_id
I have a permissions table:
id | permission
I have a role_permissions pivot table:
role_id | permission_id
A user belongs to many roles.
A role belongs to many users.
A permission belongs to many roles.
How can I query the permissions table, providing a permission and a user id. I basically want to find if a user has a specific permission.
So far I have:
$data = Permission::whereHas('roles', function($q){
//do I need another whereHas here linking roles and users?
})->where('permission', 'edit')->get();

The best way is to just join the tables. There is currently no good way in the framework to achieve this sort of chained relationship.
Permission::query()
->join('permission_role', 'permission_role.role_id', '=', 'permissions.id')
->join('roles', 'roles.id', '=', 'permission_role.role_id')
->join('role_user', 'role_user.user_id', '=', 'roles.id')
->join('users', 'users.id', '=', 'role_user.user_id')
->where('users.whatever', '=', 'something')
->where('permissions.permission', '=', 'edit')
->get();

Related

Laravel Join returns empty array

$users = DB::table('users')
->join('course', 'users.id', '=', 'course.courseId')
->join('skills', 'users.id', '=', 'skills.id')
->join('subjects', 'users.id', '=', 'subjects.id')
->get();
dd($users);
This gives me an empty result like this- items: []. I would like to join the 4 tables - users, course, skills & subjects and fetch all the data in all the 4 tables. users table has the id column(PK) named as id, course table has the id column(PK) named as courseId, skills table has its id column(PK) named as id & subjects table also has the id column(PK) named as id. The PK of all 4 tables is of the same data type biginteger. How to solve this?
Using the relations to get all the data:
The User::class has three relation methods hasMany() to the other classes (Course::class, Skill::class and Subject::class)
$users = User::with(['courses', 'skills', 'subjects'])->get();

How to get wishlist for the user who logged in

I want to do something like, a user logged in, so only those wishlists he has done for him, he can see that. What I'm getting is all wishlist for every user.
controller
$user_id = Session::get('id')
$products = DB::table('wishlist')
->join('users', 'users.id', '=', 'wishlist.user_id')
->join('product', 'product.id', '=', 'wishlist.product_id')
->select('product.*')
->get();
return view('Users.userlayout')
->with(compact('products'));
I've created Wishlist, Users, Product Model. Failed to work with Eloquent too. Wishing to get both solve.
Wishlist table column: id, product_id, user_id
I guess I have to use that $user_id in the query table but failed to do that.
NB: I've solved this problem with query builder, would like to get help
for Eloquent relationship

Laravel eloquent query model with pivot

I have a user model with pivot table role_user.
In my user table I have a field 'active'.
And a user can have multiple roles which are saved in pivot table.
How can I pull all the users where active = 1 and where user has specific role in pivot table?
Try this. This assumes you have an eloquent relationship set up between users and roles. I've only used this type of query in a hasMany / belongsTo relationship, but I think it'll work in your example as well.
$users = User::where('active', 1)->whereHas('roles', function ($query) {
$query->where('role', 'foo');
})->get();
To filter users by role use whereHas() method:
User::whereHas('roles', function($q) use ($role) {
$q->where('role', $role);
})->where('active', 1)->get();

Get All the associations that belongs to the federation where president_id = user_id In Laravel 5.2

I have 3 tables :
Federation - Association - User
An Association belongsTo a federation.
A Federation hasMany Association
Federation has a president : president_id that refers to User Model
Association has a president : president_id that refers to User Model
What I want to get:
All the associations that belongs to the federation that have the president_id == Auth::user()->id
I tried:
$associations = Association::with(['federation' =>function($query){
$query->where('federation.president_id', Auth::user()->id);
}])->get();
And also:
$associations = Association::with('federation')
->where('federation.president_id', Auth::user()->id)
->get();
But in the first case, I get all the association with the federation relationship to null, and in the second case, it doesn't know federation.president_id as it queries table 'association'
Any idea what's wrong?
with() only modifies the eager loaded relationships. In order to modify a query based on its relationships, you're looking for whereHas():
$associations = Association::whereHas('federation', function($query) {
$query->where('president_id', Auth::user()->id);
}])
->get();

Laravel elequent pivot table using where

How can I use Laravel to do something like this:
Tables:
projects:
id,name, active
pivot table: project_users
id, project_id, user_id
If I do this, I get good results, including all users of the project
Project::with('users')->where('active', '=', 1)->get();
But how can I filter on users and on active ? something like ?
Project::with('users')->where('active', '=', 1)->where('users',=,3)->get();
or ??
Project::with('users')->where('active', '=', 1)->where('users.user_id',=,3)->get();
If you want to restrict rows in the table you are eager loading then you can add constraints to the eager load where you can perform your usual QueryBuilder operations on the $query. So in your example we can do the following:
Project::with(['users' => function($query)
{
$query->where('user_id', '=', 3);
}])->where('active', '=', 1)->get();
This is assuming active is accessible to your initial table before the eager load.

Resources