Trouble with hasManyThrough in Laravel - php

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

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

Laravel 5.6.8 Eloquent and many to many + many to one joins

I have many to many connect with between user - cityarea.
I have also area which connect cityarea (One cityarea can connect only one area).
I have this database structure:
users
id
username
password
cityareas
id
name
area_id
cityarea_user
id
cityarea_id
user_id
areas
id
name
Next I have Models
User
public function cityareas()
{
return $this->belongsToMany('App\Cityarea');
}
Cityarea
public function area()
{
return $this->belongsTo('App\Area');
}
public function users()
{
return $this->belongsToMany('\App\User');
}
Area
public function cityareas()
{
return $this->hasMany('App\Cityarea');
}
QUESTION:
How I can get all users where areas.name = "South" with Eloquent ?
Thanks!!
By using whereHas, you can do:
$users = User::whereHas('cityareas.area', function ($query) {
$query->where('name', 'South');
})->get();
Jeune Guerrier solution is perfect, but you can use with() method of eloquent If you also need cityarea collection along with users collection.
$users = User::with('cityareas')->whereHas('cityareas.area', function ($query) {
$query->where('name', 'South');
})->get();
This is exactly what the belongs to many relationships is built for.
You simply have to do, Cityarea::where('name', 'South')->first()->users;
If you want to do something further with the query, e.g. sort by users created at, you can do
Cityarea::where('name', 'South')->first()->users()->orderBy('creaated_at', desc')->get();
Note that if there is no such Cityarea with name 'South', the ->first() query above will return null and therefore will fail to fetch the users.
A more performant way to do it programmatically is to use the whereHas approach as discussed in the comments below.

Get role name for admin user

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');
}`

Laravel querying multiple related models

For example: I have these models in my application.
User, Profile, Interest.
I linked the users table with the profiles table by adding the user_id column in the profiles table. And I linked profiles and interests by using a pivot table (interest_profile), Which is (as obvious) will have two columns (profile_id, interest_id).
However, I want to query the users who are associated with a profile, too see who is associated with a particular interest, In other words: "select all users who are having (in their profiles) that particular interest".
I know that I can do this with raw SQL by joining the four tables and then use (where clause).. But I want to do it the Laravel way.
Thanks in advance.
First make sure you have your relationships setup correctly on your models like:
class User extends Model
{
public function profile()
{
return $this->hasOne(Profile::class);
}
}
class Profile extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function interests()
{
return $this->belongsToMany(Interest::class, 'interest_profile');
}
}
class Interest extends Model
{
public function profiles()
{
return $this->belongsToMany(Profile::class, 'interest_profile');
}
}
Then you can use whereHas() to constrain a query by a related model and dot notation for nested relations. So your query would be:
User::whereHas('profile.interests', function($query) use ($interestName) {
return $query->where('name', $interestName);
})->get();
That would just return a collection of users. If you wanted to return their profiles and interests as well you would use with():
User::whereHas('profile.interests', function($query) use ($interestName) {
return $query->where('name', $interestName);
})
->with('profile.interests')
->get();
Assuming the User model has a relationship profile and the Profile model has a relationship interests, you can do this.
$interest_id = 1;
$users = User::whereHas('profile', function ($query) use ($interest_id) {
$query->whereHas('interests', function ($query) use ($interest_id) {
$query->where('id', $interest_id);
});
})->get();

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