Get role name for admin user - php

I have table for admin user and its roles with their relations. I want admin user listing with their respective role.
I tried below query
$adminusers = Admin::whereHas('roles', function($q)
{
$q->where('roles.name', '<>', 'Superadmin');
$q->select('roles.name as roles');
})->select('id', 'name')->where('admins.status', 'A')->where('admins.is_delete', '0')->paginate(15);
but i am not able to retrieve role name from it.
So how can i get the role name from above query?

You can't select from a whereHas closure. whereHas in this scenario is used to limit the results of admins, not to retrieve the roles.
You can query a relationship of a specific model like so:
$singleAdmin->roles()->where('roles.name', '<>', 'Superadmin')->get();
Or you can put constraints during eager loading:
Admin:::with(['roles' => function ($query) {
$q->where('roles.name', '<>', 'Superadmin');
}])->get();
https://laravel.com/docs/5.4/eloquent-relationships#constraining-eager-loads
Once eager loaded, you would need to access the collection of roles from $singleAdmin->roles.
In some cases, it makes sense to just use a join.

You can use the leftJoin method.
`$admins = Admin::leftJoin('roles','admin.role_id','=','roles.id')
->select('id', 'name')
->where('admins.status', 'A')
->where('admins.is_delete', '0')
->paginate(15)`
Or you can do it in model file
`public function role()
{
return $this->hasOne(\App\Models\Roles::class, 'id', 'role_id');
}`

Related

Laravel eloquent how to get relationships relationship?

I have Users table,Jobs table and JobStatus table. I get user jobs using relationship
return User::find($dto->getUserId())
->jobs()
This will return all user jobs as collection, not as relationship inside user model.
Now I need to get jobs statuses, but when I try this
User::find($dto->getUserId())
->jobs()
->statuses()
I get error Call to undefined method. I need to get statuses as collection so I can use where,sort by etc while getting them from db. Is there any way to do so?
I need to do something like this , but with statuses
return User::find($dto->getUserId())
->jobs()
->has('status')
->where('role','responsible')
->where('jobs.created_at','>=',Carbon::now()->subDays($dto->getPeriod())->toDateString())
->get();
To retrieve the jobs by filtering on the statuses relationship, you can do it like this:
$user->jobs()->whereHas('statuses', function ($query) {
// Perform filter on the query for jobs->statuses
})->get();
For more information about querying relationship existence: https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence
If you want to retrieve the statuses instances, you can do it like this:
$statuses = JobStatus::where('created_at', '>=', now())
->whereHas('jobs', function ($query) use ($user) {
$query->whereUserId($user->id);
})
->get();
Just use jobs without parenthesises to get the model itself :
User::find($dto->getUserId())
->jobs
->statuses;
define your statuses() function first with the relations in your model with the jobs
like many to many or one to many relationship
public function statuses(){
return $this->belongsToMany('App\Job')->withTimestamps();
}

Convert (User,Role,user_role) SQL Raw Query to Eloquent In Laravel

Respected sir,I have to fetch user role name from user_role pivot table.
I successfully done by this using Sql Raw Query.but i have to use Eloquent.
Please convert below raw sql code into Eloquent,Please help me
sir i am new in laravel.
$role = DB::table('users')
->join('user_roles', 'users.id', '=', 'user_roles.user_id')
->join('roles', 'roles.id', '=', 'user_roles.role_id')
->select('roles.name')->where('users.id', '=',$user->id)
->get();
you could try Laravel Eloquent's manyToMany relationship. It's the same exact setup as you have here with a pivot table.
Add this method to your User model
function roles {
return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
}
In your controller you could use
$roles = User::with('roles:name')->where('id', $user->id)->get();
Do try this method, you dont have to change anything to you db just adjust your model and controller.
For more info here's the documentation

Laravel - Access model attribute dynamically inside whereHas Closure

Let's say I have the relation: Role has many Users. Role and user stores a code value.
If I want to select all roles that have users with same code, how would be this query using whereHas clause?
What I tried:
$roles = Role::whereHas('users', function ($users) {
// Obviously doesn't work but it is what I need to access.
$code = $users->first()
->role
->code;
return $users->where('code', $code);
})->get();
Use this:
$roles = Role::whereHas('users', function ($query) {
$query->whereColumn('users.code', 'roles.code');
})->get();

Trouble with hasManyThrough in Laravel

Maybe dublicate, but my search hasn't any success. What we have:
Laravel 5.2, three tables
user_tariff_hours_history
id
user_tariff_id
...
user_tariff
id
user_id
...
users
id
...
I want to get user through user_tariff for current user_tariff_hours_history
As official Laravel Docs said I need to have getter with the hasManyThrough method in my user_tariff_hours_history's model https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
But result of the query with that getter didn't expected.
I get whole users table, not a user for current user_tariff_hours_history
getter:
public function getUsers() {
return $this->hasManyThrough('App\SARegistration',
'App\SAUser_tariff',
'user_id', //FK on SARegistration (users)
'id', //Local Key of ?
'user_tariff_id'); //FK on SAUser_tariff (user_tariffs)
}
Note: in docs there are 6 params for the hasManyThrough, but Laravel said it has only 5 params, than I was trying to use diffirent combo of params
Define hasManyThrough relationship called userTariffHoursHistory in User model and use whereHas():
User::whereHas('userTariffHoursHistory', function($q) use($userTariffHoursHistoryId) {
$q->where('id', $userTariffHoursHistoryId);
})->first();
Or define two hasMany() relationships (userTariffs in User model and userTariffHoursHistory in UserTariff model) and then use nested whereHas():
User::whereHas('userTariffs', function($q) use($userTariffHoursHistoryId) {
$q->whereHas('userTariffHoursHistory', function($q) use($userTariffHoursHistoryId) {
$q->where('id', $userTariffHoursHistoryId);
});
})->first();

Laravel / Eloquent : hasManyThrough WHERE

In the documentation of Eloquent it is said that I can pass the keys of a desired relationship to hasManyThrough.
Lets say I have Models named Country, User, Post. A Country model might have many Posts through a Users model. That said I simply could call:
$this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
This is fine so far! But how can I get these posts only for the user with the id of 3 ?
Can anybody help here?
So here it goes:
models: Country has many User has many Post
This allows us to use hasManyThrough like in your question:
// Country model
public function posts()
{
return $this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
}
You want to get posts of a given user for this relation, so:
$country = Country::first();
$country->load(['posts' => function ($q) {
$q->where('user_id', '=', 3);
}]);
// or
$country->load(['posts' => function ($q) {
$q->has('user', function ($q) {
$q->where('users.id', '=', 3);
});
})
$country->posts; // collection of posts related to user with id 3
BUT it will be easier, more readable and more eloquent if you use this instead:
(since it has nothing to do with country when you are looking for the posts of user with id 3)
// User model
public function posts()
{
return $this->hasMany('Post');
}
// then
$user = User::find(3);
// lazy load
$user->load('posts');
// or use dynamic property
$user->posts; // it will load the posts automatically
// or eager load
$user = User::with('posts')->find(3);
$user->posts; // collection of posts for given user
To sum up: hasManyThrough is a way to get nested relation directly, ie. all the posts for given country, but rather not to search for specific through model.
$user_id = 3;
$country = Country::find($country_id);
$country->posts()->where('users.id', '=', $user_id)->get();
$this->hasManyThrough('Post', 'User', 'country_id', 'user_id')->where(column,x);
What happen here is you get the collection in return you can put any condition you want at the end.

Categories