Laravel Left join Query explanation - laravel

I have the following tables:
Users, Studies, Study_User and Questions. The relationships are:
Many-to-Many between Users and Studies via study_user.
Studies One-to-Many Questions
Can someone please explain to me what does this query do?
Does it bring all the Questions that belongs to studies which a certain user is part of?
public function questions(){
$builder = Question::
groupBy('questions.id');
$builder = $builder
->leftjoin('studies', 'studies.id', '=', 'questions.study_id')
->leftjoin('study_users', 'studies.id', '=', 'study_users.study_id')
->where('study_users.user_id', '=', $this->id)
->where('studies.starts_at', '<=', Carbon::now())
->where('studies.ends_at', '>=', Carbon::now())
->select('questions.*');
return $builder;
}
That function is then called as
$user = Auth::user();
$user->questions()->get();
Ok and now the other question would be:
Assume that we add another table called Modules where : Studies one-to-many Modules and then Modules one-to-many Questions. So that we break the direct relationship between Studies and Questions. How the query should change to get the same results?

Related

Laravel: Eloquent wherehas doesn't exclude records with certain value

I'm creating a SNS app, in which User and Post are in many-to-many relationship, with a pivot table Favorite. Is there way to fetch all posts you didn't faved?
$posts = Post::wherehas("users",function($q)use($your_id){
$q->where('user_id', '!=', $your_id);
})->orDoesntHave('users')->get();
return $posts;
The above method was what I came up with, but it seemed not to work when more than 2 users faved the same post.
you need to fetch all posts which not related with users favorites thats mean dont have relationship with specific user , so you can use whereDoesntHave like this :-
$posts = Post::whereDoesntHave("users",function($q)use($your_id){
$q->where('user_id', '!=', $your_id);
})->get();
return $posts;

Laravel Eloquent Relationship For Multiple tables

There is 3 tables in my database: Users, Biodata, Roles.
Users and Roles are in relation with Pivot table role_user.
Users have role student & employer.
I want all the users with role student also with biodata.
I tried many times but couldnot find the solution. Hope you guys help me
$users = User::with('roles')->with('biodata')->get();
try this:
$users = User::with('biodata')->whereHas('roles', function($query) {
$query->where('name', 'student');
})->get();
Use whereHas , which is used to where query in realtion.
$users = User::whereHas('roles', function ($query) {
$query->where('name', '=', 'student');
})->with('biodata')->get();
You're looking for this: $user = User::with('roles', 'biodata')->get();

search in eloquent return query variable

Here is my problem. Please help
I have these two tables Course and Students. Students take courses. Now I want to get a specific course as shown then see in student table if this course is taken by the student.
Course table:
id
name
description
Student table:
user_id
course_id
semester
address
phone
What I tried:
$course = Course::where('name', '=', $data["id"]);
$enroll= Students::where('course_id', '=', $course["id"]);
$enroll= Students::where('course_id', '=', $course->id);
$$result=$course->toArray();
$enroll= Students::where('course_id', '=', $course["id"]);
I need to search the variable where the return value of eloquent query is stored.
For a specific user and a specific course you can get the collection of records from student table with the following query:
$courceId = 1;
$userId = 1;
$enrolled = Students::where('course_id', $courseId)
->where('user_id', $userId)
->count() > 0;
$enrolled will have true or false value
You can use eloquent relationship to obtain this in a much better way. However you can do this like the following assume your course name is $course_name:
$course = Course::where('name', '=', $course_name)->first();
if($course!=null){
$enroll= Students::where('course_id', '=', $course->id)->get();
}
and then you can check if it has data or not like
if($enroll!=[]){
echo 'this course has been taken by some student';
}
Here some student are the $enroll student.

Laravel > nested eager load with restriction

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.

Laravel database relations - get all children from more than one parent

I have users and books.
User model:
public function books() {
return $this->hasMany('Books');
}
I can do the following:
$user = User::find(1);
$books = $user->books;
Now, I want to get all books from several users with the name Brian.
So what I did is:
$users = User::where('name', 'Brian')->get();
$books = $users->books;
Of course this does not work because books() is a method of a user and not of a group of users.
How can I can all books from all users named Brian? I could loop through all Brians but that does not seem best practice.
How could I do this?
This is the perfect spot for a whereHas call:
Give books a user relationship, then simply do:
Book::whereHas('user', function($q) {
$q->whereName('Brian');
})->get();

Resources