Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hello world's avatar

Laravel-filter data where condition on pivot table

I want to filter users based on their subscription_status which s stored in a pivot table. I have Three tables users , subscription_packages , subscription_package_user

 $user=User::with(['studentDetails','subscriptionsSatus.courses'])
                        ->withPagination($offset,$perPage)
                        ->get()
                        ->sortBy('first_name')->values();

this code return the response is

 [
{
    "id": 44,
    "first_name": "Abcd Test",
    "last_name": "Test lastname",
    "student_details": null,
    "subscriptions_satus": [
        {
            "id": 1,
            "name": "Gold",
            "price": 3000,
            "user_id": "2"
            "pivot": {
                "user_id": 44,
                "subscription_package_id": 1,
                "subscription_status": "on_free_trial",
                "expires_on": null,
                "id": 9
            },
            "courses": [
                {
                    "id": 18,
                    "title": "English Grammar for Class 3",
                    "price": 400,
                    "strikethrough_price": null,
                    "status": "draft",
                    "user_id": 2,
                    "image": "http://127.0.0.1:8000/courses/1615702915.png",
                    "description": null,
                    "pivot": {
                        "subscription_package_id": 1,
                        "course_id": 18,
                    }
                }
            ]
        }
    ]
}]

i want to return only users who having subscription_status =$filter.

$filter='acive'or 'on_free_trail'

my model is

public function subscriptionsSatus()
{
    return $this->belongsToMany(SubscriptionPackage::class)->withTimestamps()->withPivot('subscription_status','expires_on','id');
}

I havetried

    $filter=$request->input('filter')??"active";
$user=User::with(['studentDetails','subscriptionsStatus.courses'])
                         ->whereHas('subscriptionsStatus', function($query) use($filter){
                             $query->wherePivot('subscription_status','=',$filter);
                         })
                            ->withPagination($offset,$perPage)
                            ->get()
                            ->sortBy('first_name')->values();

But Got error Column not found 'pivot'

0 likes
6 replies
CorvS's avatar
CorvS
Best Answer
Level 27

You are simply using the wrong "where" function inside your whereHas. Replace

$query->wherePivot('subscription_status','=',$filter);

with

$query->where('subscription_status', $filter);

and you should be fine.

Be aware, that you will get all users which have any subscription package with the given status. So if you filter for users with "active" packages and a user has a package with "active" and one with "on trail", the user will be included.

1 like
hello world's avatar

I have tried but it returns Null users .. which is not the expected output

CorvS's avatar

What are you passing as your filter? Are you sure it's a valid status?

hello world's avatar

$filter=$request->input('filter')??'active'; is there any problem with this statement? filter value default to active??

CorvS's avatar

No that's fine. I'd assume that you use it as query string (inside the URL)? Are you using TDD to develop your application or are you "testing" it by hand? Either way, have you tried hardcoding the filter to some existing value (without fetching it from the request)?

Please or to participate in this conversation.