Crud: [Bug] searchable_attributes condition not inside paranthesis in query

Created on 29 May 2020  路  3Comments  路  Source: Laravel-Backpack/CRUD

Bug report

Searchable attributes condition is not inside parenthesis in the query

What I did

protected function fetchAddress()
    {
        return $this->fetch([
            'model' => \App\Models\Address::class,
            'searchable_attributes' => ['name', 'address', 'address2', 'zip_code', 'city'],
            'query' => function($model) {
                $customer_id = 0;
                foreach ($_POST['form'] as $data) {
                    if ($data['name'] == 'customer_id') $customer_id = $data['value'];
                }
                return $model->where(function ($query) use ($customer_id){
                    $query->where('customer_id', $customer_id);
                });
            },
            'paginate' => 10,
        ]);
    }

What I expected to happen

I expected this query to be run :

select * from `addresses` where (`customer_id` = ?) and (`name` LIKE ? or `address` LIKE ? or `address2` LIKE ? or `zip_code` LIKE ? or `city` LIKE ?) limit 10 offset 0 [2, %step%, %step%, %step%, %step%, %step%]

What happened

This query is run :

select * from `addresses` where (`customer_id` = ?) and `name` LIKE ? or `address` LIKE ? or `address2` LIKE ? or `zip_code` LIKE ? or `city` LIKE ? limit 10 offset 0 [2, %step%, %step%, %step%, %step%, %step%]

What I've already tried to fix it

-

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
PHP 7.2.11 (cli) (built: Oct 10 2018 02:04:07) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

### LARAVEL VERSION:
v7.13.0@6fa69bfbd57744a5bbec5538ce483919b3fd625f

### BACKPACK VERSION:
4.1.6@cbd4143d3eb8302916012af205565cd3183f274f
Bug

Most helpful comment

Hello @dividy

Thanks again for one more bug report.

Indeed I could easily confirm this bug and hopefully I found a way to fix it.

I'v just submited PR: #2884 would you mind using that code in your project and let me know if it fixes ? From my tests is all good. I also tested with scopes.

Wish you the best,
Pedro

All 3 comments

Hello @dividy

Thanks again for one more bug report.

Indeed I could easily confirm this bug and hopefully I found a way to fix it.

I'v just submited PR: #2884 would you mind using that code in your project and let me know if it fixes ? From my tests is all good. I also tested with scopes.

Wish you the best,
Pedro

It's Saul Goodman!

You're welcome for the bug report.
More coming soon :-)

Thanks for the fast PR !

Have a nice week-end !
David

You made me google that! :smiling_imp:

Have a nice weekend David. :man_dancing:

Pedro

Was this page helpful?
0 / 5 - 0 ratings