relationships with multiple pivot table Eloquent/Laravel - php

I have 4 table
users
id
name
school
id
name
user_school
id
user_id
school_id
user_role
id
user_role
role_id
We need to find school users list whose have role id is 2
we use belongsToMany but not success
public function totalAdmin(){
return $this->belongsToMany('App\Models\User','user_school', 'school_id','user_id')->where('user_school.status',1)->where('user_school.deleted',0);
}

Assuming you have relationships:
User-Role named roles (with pivot user_role)
School-User named users (with pivot user_school)
You can do:
School::users()->whereHas('roles', function ($q) {
$q->where('id', 2);
});
You can probably transfer it in the relationship as well:
public function totalAdmin(){
return $this->belongsToMany('App\Models\User','user_school','school_id','user_id')
->whereHas('roles', function($q) { $q->where('id', 2); });
}

Related

Get only 3 records using with from many to many relationship Laravel

I'm using laravel 7 and
I have 3 tables shown below. I want first three students data instead of all student. tables have many to many relationship.
groups
id
name
students
id
name
group_student_pivot
group_id
student_id
created_at
I have below relationship in models
Groups model
public function students()
{
return $this->belongsToMany(Student::class,'group_student_pivot')->withPivot(['status'])->withTimestamps();
}
Student model
public function groups()
{
return $this->belongsToMany(Group::class,'group_student_pivot')->withPivot(['status'])->withTimestamps();
}
$groups = Group::whereIn('id',$groupIds)->with('students')->get();
In above query I want first 3 students data instead of all students.
You can get 3 records like this:
$groups = Group::whereIn('id',$groupIds)->with('students', function($q){
$q->take(3);
})->get();
You can use with and whereHas method
$groups = Group::with('students')->whereHas('students', function($q){
$q->take(3);
})->whereIn('id',$groupIds)->get();

Connect pivot table with multiple tables in Eloquent

I have this model:
User:
id (int)
name (varchar)
Role:
id int
name (varchar)
User_Role
id (int)
id_role (int)
id_user (int)
id_company (int)
What I need using Eloquent (Laravel 7), is to query user_role with the id_company:
This is my relationship into User model
public function rol() {
return $this->belongsToMany('\App\Models\Role','user_role','id_user','id_role');
}
This is my relationship into Role model:
function user() {
return $this->belongsToMany('\App\Models\User','user_role','id_role','id_user');
}
Is it possible to query the pivot table like this:
$user->rol->where(["id_company" => $params["id_company"]])->toArray();
You need to first include the pivot table columns in the relationship:
public function roles() {
return $this->belongsToMany('\App\Models\Role','user_role','id_user','id_role')
->withPivot('id_company');
}
Then you can query on the pivot table column:
$user->roles()->wherePivot('id_company', $params["id_company"])
->get()
->toArray();

querying based on the additional pivot table column in laravel

I have a User Model with the columns id, name, email and
a Course Model with the columns id, name, code and
a pivot table course_user.
Course and User has many-to-many relation
course_user table contains columns course_id, user_id, section
While taking courses, some users will be listed as 'Pending' in pivots section column and some users will be listed as 'A', 'B' etc in section column.
Question: I want to get those courses which are taken by at least one user and section is 'Pending'
So far, I've tried these:
In Course Model :
public function users(){
return $this->belongsToMany(User::class)
->withPivot('section');
->wherePivot('section', 'Pending');
}
Another Approach I've tried in course Model:
public function pendingUsers(){
return $this->belongsToMany(User::class)
->withPivot('section')
->wherePivot('section', 'like', 'Pending');
}
But all of these are giving me all of the courses.
Now, How can this job be done?
Define the relationship:
public function users()
{
return $this->belongsToMany(User::class)->withPivot('section');
}
Then:
Course::whereHas('users', function($q) {
$q->where('course_user.section', 'Pending');
})
->get();

Laravel Eloquent Relationship custom query

I have 2 tables: USERS and SUBJECTS
The relationship between USER and SUBJECT is many to many.
In the User.php and Subject.php models, I defined:
User.php
function subjects() { return $this->belongsToMany('App\User'); }
Subject.php
function users() { return $this->belongsToMany('App\Subject'); }
The pivot table is subject_user and it has 3 columns:
subject_id, user_id, finished
The finished value can only be between 0 and 1.
Now I know that when I want to select all the subjects that an user studied, I have to write $user->subjects.
But what if I want to select all the subjects that an user studied and the finished value in the pivot table is equal to 1?
You need to add "withPivot()" to your relationship definitions, like this:
function subjects() { return $this->belongsToMany('App\User')->withPivot('finished'); }
function users() { return $this->belongsToMany('App\Subject')->withPivot('finished'); }
Then you can do:
$user->subjects()->where('finished', 1)->get();
You will need to eager load that relationship and use the wherePivot method.
$user = User::with(['subjects' => function($q) {
$q->wherePivot('finished', 1);
}])->fine($user_id);

How to filter many to many structure in Laravel

I got a many to many user and role structure
users
id
name
roles
id
name
role_user
user_id
role_id
Model
User.php
public function roles() {
return $this->belongsToMany('Role');
}
Role.php
public function users() {
return $this->belongsToMany('User');
}
There are two data admins and members in roles table, I would like to know to to filter users which role is admins.
This should give you all users who are admins.
$users = User::whereHas('roles', function($q) {
$q->where('name', '=', 'admins');
})->get();
You can see more information on the has() method at http://laravel.com/docs/eloquent#querying-relations

Categories