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

amun's avatar
Level 1

Please HELP ME, the query doesn't work JOIN and WHERE clause on the joined table's column

$filter->where(function ($query) {
                $query->leftjoin('customers AS c', function($join){
                    $join->on('booking.customer_id', '=', 'c.id')
                    ->where('c.first_name', 'like', "%{$this->input}%");
                })->dump();
            }, 'Customer has booking')->placeholder('search by first name Like');

Dump data :

"select * from `booking` left join `customers` as `c` on `booking`.`customer_id` = `c`.`id` and `c`.`first_name` like ?"

I tried to run on MySQL phpMyAdmin, it works, but the result on website response the same

0 likes
8 replies
tykus's avatar

Should not be wrapped in a WHERE

$filter->leftJoin('customers', function($join) {
    $join->on('booking.customer_id', '=', 'customers.id') 
        ->where('customers.first_name', 'like', "%{$this->input}%");
})->toSql();
tykus's avatar

@amun

It must be wrap in WHERE

why; what is $filter???

amun's avatar
Level 1

@tykus I am using: laravel-admin -> model-grid-filters

tykus's avatar

@amun okay... never used this package. What exactly does it mean the query doesn't work JOIN and WHERE clause on the joined table's column; what is expected, and what are you actually getting???

1 like
amun's avatar
Level 1

@tykus i have 2 tables: Customer and Booking. A customer has many booking. I want to query list of booking where the first name in customer table contain input keyword.

Tray2's avatar

@amun So this is the result you are looking for?

Customer::where('first_name', 'like', '%' . $search . '%')
	->with('bookings')
    ->get();
1 like
amun's avatar
Level 1

Sorrry, below is my full code

$grid->filter(function ($filter) {
            $filter->where(function ($query) {
                $query->join('customers AS c', function ($join) {
                    $join->on('booking.customer_id', '=', 'c.id')
                        ->where('c.first_name', 'like', "%{$this->input}%");
                })->dump();
            }, 'Customer has booking')->placeholder('search by first name Like');

            $filter->disableIdFilter();
        });

Please or to participate in this conversation.