I am working with two models, UserType and User - UserType hasMany User.
I am trying to retrieve a list of Users associated with a UserType that has the property receive_email set to 1 (true).
I have tried:
$userGroups = UserType::with(['Users' => function($query) {
$query->whereReceiveEmail(1)->whereNotNull('email')->whereNull('status');
}])->whereIn('id', [10, 1])->get();
and the Where clause seems to be totally ignored. From the Laravel 4.2 docs -
$users = User::with(array('posts' => function($query)
{
$query->where('title', 'like', '%first%');
}))->get();
I have seen many people say that this is not the correct way to use eager loading constraints but I really do not know what that would be, they do not seem to do anything. So, the short question, how can I retrieve a listing of Users with receive_email set to 1 through the UserType relation?
UPDATE
Can someone explain to me what the example code from the docs above is supposed to do? I'm assuming that it is supposed to return Posts associated with Users that match the constraint of having a title LIKE "first." In my case, I'm trying to find Users associated with UserTypes where each User has receive_email set to 1. The only significant differences between my code and the example code is that I am applying whereIn() and the model names are different.
So, with the results from the example, would the following be true?
foreach ($users as $user) {
foreach ($user->posts as $post) {
// matching posts with titles LIKE "first"
}
}
If you're after a list of users, then I suggest you actually start with that model and make use of whereHas to filter by user type:
$users = User::where('receive_email', 1)
->whereNotNull('email')
->whereNull('status')
->whereHas('UserType', function($q){
$q->whereIn('id', [1, 10]);
})
->get();
And actually, since the user type id should exist as foreign key in the users table, you don't even need whereHas:
$users = User::where('receive_email', 1)
->whereNotNull('email')
->whereNull('status')
->whereIn('user_type_id', [1, 10]);
->get();
For RosterMember it's basically the same. Although now you have to use whereHas since it's a many-to-many relation:
$rosterMembers = RosterMember::where('receive_email', 1)
->whereNotNull('email')
->whereHas('UserType', function($q){
$q->whereIn('user_type_id', [1, 10]);
})
->get();
I'm not 100% familiar with relationships, they've always been a bit tricky. But from what I understand, you want all the Users in UserType 1 and 10 that have receive_emails set to 1. So, this should work:
$result =
UserType::whereIn("id", array(1, 10))
->first()
->users()
->where("receive_email", "=", 1)
->whereNotNull("emails")
->whereNull("status")
->get()
;
What this should do is return all the accessible fields from both id 1 and 10 of UserType as well as all fields from the User table. If you run a dd($result) on this query, you should see an entry for UserType id 1 connected to all the Users that have receive_email set to 1, and another set for UserType id 10.
I can't guarantee that this will work without seeing your UserType.php and User.php classes, as the relationships might not be set, but if you followed Laravel convention:
public function users(){
return $this->hasMany("User");
}
and the inverse
public function userType(){
return $this->belongsTo("UserType");
}
then it should work. Hope this helps! Also, I'm sure there are better ways to accomplish this, but this is what I came up with, and it seems to work on some of my existing projects with relationships.
Cheers!
Related
Consider the following case.
we have the Users table and Tasks table. they are in relation with belongsToMany with table task_user.
How to get the list of all users who are not under any task? i.e. their user_id is not at all under that given task or even in the task_user table.
why I need this is because like this we can only provide a list of users who are yet to be assigned a task. the task will be assigned to users and not a single user at a time.
Editing_____________
also how to filter with users based on group table? below is not working
$users = Group::with(['subscribers' => function ($q){
$q->doesntHave("tasks");
}])->whereId($gid)->latest()->get();
Assuming you've named your relationships properly, you should be able to use doesntHave("tasks"):
$tasklessUsers = User::doesntHave("tasks")->get();
doesntHave() checks for the non-existence of the supplied relationship ("tasks", in this case) and returns all objects that pass this check.
If your function name is different, use that, but the relationship should be:
User.php:
public function tasks(){
return $this->belongsToMany(Task::class, "task_user");
}
Edit: doesntHave() is the simple version, whereDoesntHave() allows a custom query. See https://laravel.com/docs/5.8/eloquent-relationships#querying-relationship-absence for full details.
Second Edit:
As stated in the comments below, with() will not filter the Model it is being called on, so this query won't work as you'd expect:
$users = Group::with(['subscribers' => function ($q){
$q->doesntHave("tasks");
}])->whereId($gid)->latest()->get();
To fix this, use a chained doesntHave() query:
$query = Group::doesntHave('subscribers.tasks')
->where('id', '=', $gid)
->latest()
->first();
// OR
$query = Group::whereHas('subscribers', function($subQuery){
$subQuery->doesntHave('tasks');
})->where('id', '=', $gid)
->latest()
->first();
$users = $query->subscribers; // Return `users` (aliased to `subscribers`)
Either approach will check the existence of subscribers that don't have any associated tasks relationship, and also only return where id is $gid.
Note: Used first() for the queries, as using id in a query should only ever return a single Group record, and get() is for returning multiple records in a Collection
I'm building a Laravel page on which I want to show a list of lessons. Which lessons should be on the page is filtered by three criteria (of which all should be true):
The lesson is active, ie "where('active', true)". Simple enough.
The lesson is part of a track that the user has chosen. Models are set up with belongsToMany() (it is a many-to-many relationship), so I can get these lessons by a simple $track->lessons.
This is where it gets tricky. Some lessons should only be visible to users with certain titles (ie there is a many to many between titles and lessons). I can get the lessons with the correct title requirement using Auth::user()->title->lessons.
Question is how I get all this together. The best I've come up with this far is the following:
$title = Auth::user()->title;
$lessons = Lesson::where('active', true)
->whereIn('id', $track->lessons->pluck('id'))
->where(function ($query) use($title) {
$query->whereIn('id', $title->lessons->pluck('id'))->orWhere('limited_by_title', false);
})
->get();
...which is crap ugly, clearly suboptimal and (for some reason I really don't understand) also won't work (I don't get the lessons my title entitles me to in my list). Been struggling for quite some hours now, I get the feeling that I'm overcomplicating, first plucking id's and then using them in a whereIn() can't possibly be a good way of doing this.
So I can easily enough get a collection of lessons in the track, and I can get a collection of lessons belonging to the title, but how do I get all objects that exist in both those collections?
Using whereHas() is the answer to your concerns about plucking IDs. Instead of running additional queries to retrieve IDs, whereHas() will attach the constraint to the original query as a subquery on the related tables.
Breaking the query down to its parts:
1: Answered
2: Assuming the inverse of $track->lessons is $lesson->tracks, and $track is coming from code you didn't include:
$lessons = Lesson::whereHas('tracks', function ($query) use ($track) {
$query->where('id', $track->id);
})
3: Assuming the inverse of $title->lessons is $lesson->titles:
$lessons = Lesson::where(function ($query) use ($title) {
$query->whereHas('titles', function ($query) use ($title) {
$query->where('id', $title->id);
})
->orWhere('limited_by_title', false);
})
Combined back into one:
$track = ???;
$title = Auth::user()->title;
$lessons = Lesson::where('active', true)
->whereHas('tracks', function ($query) use ($track) {
$query->where('id', $track->id);
})
->where(function ($query) use ($title) {
$query->whereHas('titles', function ($query) use ($title) {
$query->where('id', $title->id);
})
->orWhere('limited_by_title', false);
})
->get();
If this still doesn't give the results you were expecting, you can examine the full query being run by replacing get() with toSql(). Sometimes working from the ORM as a starting point instead of the SQL can lead you down the wrong path. For even more detail to debug and understand the queries being run, you can enable query logging: https://laravel.com/docs/5.7/database#listening-for-query-events
intead of where use whereHas on "titles" relationship
$title = Auth::user()->title;
$lessons = Lesson::where('active', true)
->whereIn('id', $track->lessons->pluck('id'))
->whereHas('titles',function ($query) use($title) {
$query->whereIn('id', $title->pluck('id'))
->orWhere('limited_by_title', false);
})->get();
First of all complicated, really complicated. Your table structure needs serious modification to make it easier.
However, considering you don't want to go down that road, you could do it simpler by using join
Assuming you have following table structure:
users
titles (has user_id foreign key)
lessons (has title_id foreign key)
tracks (has lesson_id foreign key)
$trackName = $request->input('track_name');
$title = Auth::user()->title;
$lessons = Lesson::join('tracks', 'lessons.id', '=', 'tracks.lesson_id')
->join('titles', 'lessons.title_id', '=', 'titles.id')
->where('lessons.active', true)
->where('tracks.track_name', $trackName)
->where(function ($query) use($title) {
$query->where('titles.id', $title->id)->orWhere('lessons.limited_by_title', false);
});
dd($lessons);
That is of course if your users table and titles have one to one relationship otherwise pluck all title_ids and use whereIn instead of where for titles.id query.
I hope you have enough understanding of laravel framework to understand and implement this solution.
Sorry, I don't have enough time to proofread or give more details.
Good luck!
Good luck if you need pagination after that :p I doubt simple ->paginate() will work :D
I hope it helps
I have a table with user data (users) and a table with prices (prices).
My prices table can contain multiple prices pr. user since I want to keep historical data.
I've defined my relation as a one-to-one
$this->hasOne("App\Model\Price","userid","id")->orderBy("id","desc")->take(1);
to allow me to see the users current price.
What I want to do now, is to select every user that has a current price of 100, but how do I do this? I know I could go for a left join, but as I read the documentation, it should be possible without a left join.
I've built a pseudo-query to explain what I'm after;
User::with("price")->where("prices.price","100")->get();
I've read through the documentation (Eloquent: Querying relationships), but that doesn't seem to be useful to my question.
I've also read several questions here on SO but unfortunately to no avail.
You may try this:
$currentPrice = 100;
$users = User::whereHas('price', function($query) use ($currentPrice) {
$query->where('price', $currentPrice); // price is the field name
})
->with("price")->get();
Since you have more than a single price for per user then you may also declare another relationship method to get all the price models instead of one and you may do it using something like this:
// In User model
public function prices()
{
return $this->hasMany("App\Model\Price", "userid", "id");
}
In this case, with::price will give you the last single record and with::prices will give you all the related prices. So, if you want then you may write something like the following to get all users with their all related prices who has the (latest/current) price of 100:
$currentPrice = 100;
$users = User::whereHas('price', function($query) use($currentPrice) {
$query->where('price', $currentPrice); // price is the field name
})
->with("prices") // with all prices
->get();
You can use the combination of whereHas() and with() as:
$users = User::whereHas("price", function($q) use ($currentPrice) {
$q->where("price", $currentPrice);
})
->with(["price" => function ($q) {
$query->where("price", $currentPrice);
})
->get();
Assumed we've got users, friends and restaurants. Don't want to go to deep into the Model and relationship setup.
While me as a user is logged in: How can I get all friends who are "customers" of the restaurant?
I've got this and it's already working:
$friends = array_dot(Auth::user()->friends()->select('users.id')->get());
$customers = Restaurant::with(['users' => function($query) use($friends) {
$query->whereIn('users.id', $friends);
}])->find(restaurant_id);
But is this even possible with a single query?
It sounds like you want to find all of your friends that have a relationship to the restaurant. If so, you're looking for whereHas(). General idea:
$restaurantId = 1;
$user = Auth::user();
$friendCustomers = $user->friends()->whereHas('restaurant', function ($query) use ($restaurantId) {
$query->where('id', $restaurant_id);
})->get();
You can read more about querying relations, and whereHas, here.
I've got a situation where I've got Posts, Users and Comments.
Each comment stores a post_id and a user_id. What I want to do is get all of a user's comments on a particular post, so that I can do a call like this:
$comments = Auth::User()->comments(post_id=x)->text
(where I know what x is)
I have:
User->HasMany(comments)
Comments->HasOne(User)
Comments->HasOne(Project)
Project->HasMany(comments)
I feel like there needs to be a where or a has or a wherehas or something thrown in.. the best I can manage is that I pull Auth::User()->comments into an array and then search through the array until I find the matching post ID.. that seems wasteful.
with doesn't apply any join, so you can't reference other table.
You can use this:
// User model
public function comments()
{
return $this->hasMany('Comment');
}
// Comment model
public function scopeForPost($query, $postId)
{
$query->where('post_id', $postId);
}
// then you can do this:
Auth::user()->comments()->forPost($postId)->get();
Alternatively you can eager load comments with constraint:
User::with(['comments' => function ($q) use ($postId) {
$q->where('post_id', $postId);
}])->find($someUserId);
// or exactly the same as above, but for already fetched user:
// $user .. or
Auth::user()->load(['comments' => function ($q) use ($postId) {
$q->where('post_id', $postId);
}]);
// then you can access comments for $postId just like this:
Auth::user()->comments; // collection
When you need to filter your relations, you just have to do it in your Eloquent query:
$data = User::with('posts', 'comments')
->where('users.id', Auth::User()->id)
->where('posts.id', $postID)
->get();
Then you can
foreach($data->comments as $comment)
{
echo $comment->text;
}
Your Comments table would have foreign keys Post_Id and User_ID
To Access all the comments of a particular post from a particular user , can you try this way?
Comment::select('comments.*')
->where('comments.user_id', Auth::user()->id)
->leftJoin('posts','posts.id','=','comments.post_id')
->leftJoin('users','users.id','=','comments.user_id')
->get();
Am sure there is better way to achieve it, but this should give you desired results.
Note use aliases if you have conflicting column names
Let me know if this worked.