querying based on the additional pivot table column in laravel

Misbah Ahmad

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?

Alexey Mezenin

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel - Save additional column value to pivot table

Laravel Eloquent - querying pivot table

How to add additional column relationship in pivot table in Laravel

Laravel - Get additional column value from pivot table

How to add data to additional column in pivot table in laravel

Laravel - Access additional column data within a pivot table

SQL Pivot Table with Additional Column

laravel pivot table with additional parameters

Laravel querying data through a pivot table

How to use additional fields in pivot table Laravel

Update additional pivot table value in Laravel 5

Filling an additional field in pivot table in Laravel 5.4

Querying 'across' pivot table

Pivot Table with Additional Columns

Oracle SQL Pivot query additional column from same table

How to attach different value for additional field in pivot table Laravel 5

SQL PIVOT a table based on a column with ID's

Loop Pivot Table Filter based on values in column

Retrive data based on pivot table in laravel

Laravel filter with pivot table based on user id

getting the value of an extra pivot table column laravel

How to compare on Laravel pivot amounts with table column

Querying a pivot table through a relationship

Laravel can't access pivot additional column from pivot model itself

Pandas - Pivot Table based on Column A while ensuring order in Column B

Querying a table with LONG column

how to replace a true value based on column name and additional table?

Creating pandas pivot table with new column names based on condition

Calculate average based on a value column (count) in a pivot table