Crud: Laravel backpack Search

Created on 26 Aug 2019  路  7Comments  路  Source: Laravel-Backpack/CRUD

Hello guys, I'm using Laravel backpack in my project. I鈥檓 trying to enable the case-insensitive search functionality. here is my code
$this->crud->addColumn([ 'name' => 'name', 'label' => 'name', 'searchLogic' => function ($query, $column, $searchTerm) { $query->orWhere('name', 'like', '%'.$searchTerm.'%'); } ]);
somehow it is not working as expected. any help that would be great. Thanks!

triage

Most helpful comment

@brynnb here is the fix

$this->crud->addColumn([
           'name' => 'name',
           'label' => 'name',
           'searchLogic' => function ($query, $column, $searchTerm) {
               $query->orWhere('name', 'ilike', '%'.$searchTerm.'%');
           }
       ]);

Cheers! :)

All 7 comments

Fixed. NVM

@sandeepchowdary7 This is the first result for this issue - might be helpful for future people if you included what the fix was :)

@brynnb here is the fix

$this->crud->addColumn([
           'name' => 'name',
           'label' => 'name',
           'searchLogic' => function ($query, $column, $searchTerm) {
               $query->orWhere('name', 'ilike', '%'.$searchTerm.'%');
           }
       ]);

Cheers! :)

Thanks Sandeep! Additionally I found in another result that the issue is sometimes the database collation - apparently some are case sensitive and others aren't. I believe this was the issue for me (two identical sites deployed on two different databases - MySQL and Postgres - and one was being case sensitive and the other wasn't).

@brynnb I'm using Postgres DB. The above code was fixed my problem.

It's a good solution for postgres, but it will throw 500 errors if using MySQL which doesn't have "ilike". For a project like mine that may be deployed on a variety of databases, this wouldn't work - but using the migrations to ensure the collation for columns is case insensitive will work regardless of database.

Thanks again!

Thanks for sharing your solution @sandeepchowdary7 !

Was this page helpful?
0 / 5 - 0 ratings