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

eliekhazzaka's avatar

laravel eloquent query

guys I wrote a query I have gymClientPurchase.appointment there are appointments in it and the relation is one too many I need to send a request: status = Pending if there is one appointment Completed and other appointments are Pending it should return the appointments pending and completed and if the request status = Completed and all the appointments are completed it shouldn't return them any help please?

 public function getMembershipByClientID(Request $request)
    {
        $getMembershipByClientID = GymMembership::with(
            ['gymClientPurchase' => function ($builder) use ($request) {
                $builder->where(['client_id' => $request->client_id])
                    ->where('payment_required', '=', 'no');
            }
                , 'service', 'merchant', 'gymClientPurchase.gymClients',
                'gymClientPurchase.appointment', 'gymClientPurchase.commonDetails'

            ])
            ->when($request->client_id, function ($builder) use ($request) {
                $builder->whereHas('gymClientPurchase', function ($builder) use ($request) {
                    $builder->where('client_id', $request->client_id);

                });
            })
            ->when($request->status, function ($builder) use ($request) {

                $builder->whereHas('gymClientPurchase.appointment', function ($builder) use ($request) {
                    $builder->where('status', $request->status);

                    $builder->WhereDoesntHave('gymClientPurchase.appointment', function ($builder) use ($request) {
                        $builder->where('status', '=','Completed');
                    });
                });
                return $builder;
            })
            ->get();

        return $this->showAll($getMembershipByClientID);
    }
0 likes
0 replies

Please or to participate in this conversation.