Laravel - Eager Loading get only first result - laravel-4

I need to get all the user from my database excluding only the user that belongs to Admin and Staff group and I need, for each of them, to get their profile so I used the Eager Loading:
$data['users'] = User::with('profile')->join('role_user', function($join){
$join->on('role_user.user_id', '=' , 'users.id');
})
->join('roles', function($join){
$join->on('roles.id', '=', 'role_user.role_id');
})
->whereNotIn('roles.name', ['Admin', 'Staff'])
->withTrashed()
->paginate(10);
My DB Table:
users:
id | email | password
profile:
user_id | first_name | last_name
roles:
id | name
role_user:
user_id | role_id |
The problem is that the code above works with user infos but shows only the first profile so I have the same first_name and last_name with different email

something like this should work:
$users = \User::with('profile', 'roles')
->whereRaw('roles.name', '!=' 'Admin', 'OR', 'roles.name', '!=', 'Staff')
->withTrashed()->paginate(10);

Related

Order by relationship with pagination laravel eloquent

I have the relationship below, an application belongs to a user.
Users table
ID | Name
1 | Danny
2 | Mark
Applications table
ID | user_id
1 | 1
2 | 2
Application.php
public function user()
{
return $this->belongsTo(User::class);
}
I'm trying to sort the application by user's name and paginate the result. I tried the code below, the pagination is working but the order by doesn't have any effect.
$query = Application::query();
$query = $query->with(['user' => function ($query){
$query->orderBy('name', 'DESC');
}]);
$query = $query->paginate(10);
Try using inner join and order by relation column:
$applications = Application::select('*')
->join('authors', 'applications.user_id', '=', 'users.id')
->orderBy('authors.name', 'DESC')
->paginate(10);
Try this:
Application::join('users', 'applications.user_id', '=', 'users.id')
->orderBy('users.name','desc')
->with('users')
->get();

How to get post which have multiple table relationship?

my database scheema
Table POST
| id | title | content |
Table SharedGroup
|id | title | slug
Table Post_SharedGroup
|id| post_id | shared_group_id|
i want to make a query in laravel that i give slug of sharedgroup and get post of it
$data = Post::whereHas('sharedGroup',function($query) use($slug){
$query->whereIn('slug',$slug);
})->get();
where $slug is your slug array given by you
Model Post
public function sharedGroup(){
return $this->belongsToMany('App\SharedGroup');
}
Model SharedGroup
public function post(){
return $this->belongsToMany('App\Post');
}
You seem to just want a basic join between the three tables:
$posts = DB::table('POST p')
->join('Post_SharedGroup ps', 'p.id', '=', 'ps.post_id')
->join('SharedGroup s', 'ps.shared_group_id', '=', 's.id')
->whereIn('s.slug', array(1, 2, 3)) // match slug values (1, 2, 3)
->select('p.*')
->get();

2 id column after JOIN in Laravel. How to access to both IDs in Controller and Blade?

After join I got table with 2 ID column. Now I don't know how to access in Controller and Blade both of IDs.
In Blade when I try {{$value->id}} and it gets second ID.
$users = User::where('user_id', $user_id)
->join('tagged', 'tagged.taggable_id', '=', 'users.id')
->orderBy('users.id', 'asc')
->get();
Gives me:
-------------------------------------------------------
| id | name| phone | id | taggable_type | taggable_id |
-------------------------------------------------------
How can I get both IDs in Controller and Blade?
$users = DB::table('users')->where('user_id','=', $user_id)
->select('users.*','tagged.id as tagged_id') //add fields required from tagged table in this row
->join('tagged', 'tagged.taggable_id', '=', 'users.id')
->orderBy('users.id', 'asc')
->get();
Use $users->tagged_id and $users->id in view

Raw SQL inside eloquent query

I have a users table, roles table and a user_role table.
User
id | name
Roles
id | title
User_Role
user_id | role_id
I need an eloquent query that finds if a user has all roles.
So far I have:
$userId = 1;
$role = array('admin', 'public');
$data = User::whereHas('roles', function($q) use ($role){
$q->whereRaw($q->whereIn('title', $role)->count() .' = '.count($role));
})->find($userId);
But I get the error:
Unknown column user.id in where clause.
Where am I going wrong?
This should work.
The query should return null if the user doesn't have the specified roles.
$userId = 1;
$role = array('admin', 'public');
$data = User::whereHas('roles', function($q) use ($role){
$q->whereIn('title', $role);
}, '>=', count($role))->find($userId);

Querying two many to many relationships

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();

Resources