I have a set up like this,
QUIZ MODEL
public function scores()
{
return $this->hasMany('App\Score');
}
SCORE MODEL
public function quiz()
{
return $this->belongsTo('App\Quiz');
}
public function user()
{
return $this->belongsTo('App\User');
}
USER MODEL
public function scores()
{
return $this->hasMany('App\Score');
}
Some background, a quiz should only be playable by a user if that user does not already have a score for said quiz, what I am wanting to do is that if a user has a relationship with a quiz via having a score I want to stop that quiz being return in the query, here is my attempt,
$quiz = Quiz::with('questions.answers')
->has('scores.user', 2)
->whereDate('date_playable', '=', $date)
->first();
However this returns no quizes regardless of whether the user has a score for it or not. Can anyone enlighten me on how to only return quizes that a user does not currently have a score for?
You are currently searching for a quiz that does not have more than 2 scores for any user.
What you need is whereDoesntHave instead:
$quiz = Quiz::with('questions.answers')
->whereDoesntHave('scores', function ($query) use ($user) {
$query->where('user_id', $user->id);
})
->whereDate('date_playable', '=', $date)
->first();
Where $user is the App\User instance that you are querying for.
There could be multiple approaches to achieve that outcome. I am thinking about creating a many to many relationship between Quizzes and Users, taking scores as the middle table.
User
{
public function quizzes()
{
return $this->belongsToMany(Quiz::class, 'scores');
}
}
Then to get the desired quiz:
$quiz = Quiz::with('questions.answers')
->whereKeyNot($user->quizzes()->pluck('id')->all())
->whereDate('date_playable', '=', $date)
->first();
Related
I have builded a system in larvel and vue js, so i have some users who can have access on this system. Now i need for each user they to see only data belongs to his team where hi is on this team. I have some workers, teams, and jobs. When a worker is created, a user is also created in the users table. the user that is created takes a field called worker_id that corresponds to the id of the worker that is created. Now this worker is in a team, and several jobs have been assigned to this team. I want that when this user who has the same worker_id with worker->id logs in, he can only see the jobs assigned to his team.
jobcontroller function to show jobs
$jobs = Job::with(['teams', 'clients'])
->whereHas('teams', function ($q) {
return $q->where('id', auth()->user()->worker_id); //i know there is wrong, iam just trying
})
->orderBy('id', 'DESC')->paginate($paginate);
return JobResource::collection($jobs);
jobrelationships
public function teams()
{
return $this->belongsToMany(Team::class);
}
public function clients()
{
return $this->belongsToMany(Client::class);
}
teamrelationship
public function jobs()
{
return $this->belongsToMany(Job::class);
}
workerrelationsip
public function teams()
{
return $this->hasOne(Team::class);
}
In your example you are attempting to query for teams by using the worker ID, rather than querying for teams using the worker's team ID.
$workerId = auth()->user()->id;
$jobs = Job::with('teams.workers')
->whereHas('teams.workers', function ($q) { return $q->where('id', $workerId); })
->orderBy('id', 'DESC')
->paginate(10);
return JobResource::collection($jobs);
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.
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();
In my application, I have setup a User model that can have subscribers and subscriptions through a pivot table called subscriptions.
public function subscribers()
{
return $this->belongsToMany('Forum\User', 'subscriptions', 'subscription_id', 'subscriber_id');
}
public function subscriptions()
{
return $this->belongsToMany('Forum\User', 'subscriptions', 'subscriber_id', 'subscription_id');
}
My question is, what relationship should I use to get a list of paginated Post models (belong to a User) from the User's subscriptions?
You can use the whereHas method to filter based on relationships. Assuming your Post model has a user relationship defined, your code would look something like:
// target user
$user = \App\User::first();
$userId = $user->id;
// get all of the posts that belong to users that have your target user as a subscriber
\App\Post::whereHas('user.subscribers', function ($query) use ($userId) {
return $query->where('id', $userId);
})->paginate(10);
You can read more about querying relationship existence in the documentation.
You can do something like this
\App\Post::with(['subscriptions' => function ($query) {
$query->where('date', 'like', '%date%');
}])->paginate(15);
Or without any conditions
\App\Post::with('subscriptions')->paginate(15);
I would like to display the posts of everyone the current user follows, ordered by date desc.
I have a many to many relationship supplying all the people the user is following.
$users = User::find(Auth::user()->id)->follow()->get();
I have a one to many relationship displaying the posts for any user.
$updates = App\User::find(?????)->updates()->orderBy('created_at', 'desc')->get();
The question mark's shows where the followers ID's need to be placed.
I can put the above query inside the for each loop but that obviously works its way through each follower rather than all posts in date order.
I suspect I may need to set a new relationship and work from the beginning. Can anyone advise.
User Model
public function updates()
{
return $this->hasMany('App\update');
}
/**
* User following relationship
*/
// Get all users we are following
public function follow()
{
return $this->belongsToMany('App\User', 'user_follows', 'user_id', 'follow_id')->withTimestamps()->withPivot('id');;;
}
// This function allows us to get a list of users following us
public function followers()
{
return $this->belongsToMany('App\User', 'user_follows', 'follow_id', 'user_id')->withTimestamps();;
}
}
Update Model
public function user_update()
{
return $this->belongsTo('App\User');
}
Thank you.
Since you want the posts, it is probably going to be easier starting a query on the Post model, and then filter the posts based on their relationships.
Assuming your Post model has an author relationship to the User that created the post, and the User has a follower relationship to all the Users that are following it, you could do:
$userId = Auth::user()->id;
$posts = \App\Post::whereHas('author.follower', function ($q) use ($userId) {
return $q->where('id', $userId);
})
->latest() // built in helper method for orderBy('created_at', 'desc')
->get();
Now, $posts will be a collection of your Post models that were authored by a user that is being followed by your authenticated user.