How do I overwrite the search() logic in an entity crud controller? #1327
Replies: 3 comments
-
I can help you with overwriting the search() method in your EntityCrudController. Based on the error you're seeing, there seems to be an issue with your search query not properly joining the tables before trying to access columns. Overwriting the search() methodTo overwrite the search() method in your EntityCrudController, you need to create a method with the same name in your controller. Here's how you can do it: public function search()
{
$this->crud->applySearchTerm(request()->input('search.value'));
$this->crud->applySearchScope();
// Get entries
$entries = $this->crud->getEntries();
// Here you can modify the query as needed
// For example, to properly join the clients table:
$entries = $entries->leftJoin('clients', 'clients.id', '=', 'notes.client_id');
// Count total and filtered entries
$totalEntryCount = $this->crud->model->count();
$filteredEntryCount = $entries->toBase()->getCountForPagination();
// Skip and take for pagination
$entries = $entries->skip(request()->input('start'))
->take(request()->input('length'))
->get();
return $this->crud->getEntriesAsJsonForDatatables($entries, $totalEntryCount, $filteredEntryCount, request()->input('start'));
} The issue with your current searchLogicLooking at your error, the problem is that your query is trying to access $query->leftjoin('clients', 'client.id', '=', 'client_id') You're trying to join The correct join should be: $query->leftjoin('clients', 'clients.id', '=', 'notes.client_id') Alternative approach using searchLogicIf you prefer to keep using the searchLogic attribute, you can fix it like this: 'searchLogic' => function ($query, $column, $searchTerm) {
return $query
->leftjoin('clients', 'clients.id', '=', 'notes.client_id')
->whereFullText(
['clients.lastname', 'clients.firstname', 'clients.middle_names', 'clients.alias', 'clients.pref_lastname', 'clients.pref_firstname', 'clients.alt_spellings'],
$searchTerm,
['mode' => 'boolean'],
);
}, Note that I've added For more information on custom search logic in Backpack, you can refer to the community forum discussion on overriding search in CrudPanel List. Hope this helps! ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Github Issue (closed): How to customize search query? |
Beta Was this translation helpful? Give feedback.
-
While this is a great answer (and it looks like I've asked this question before, so it probably needs adding to the official documentation?!), when using the searchLogic attribute I'm still getting the mysql error that's showing the query doesn't have the join. I'm just about to see what/if the search() function does any better! |
Beta Was this translation helpful? Give feedback.
-
That doesn't seem to work or is a complete solution. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey All
Reading on this page https://backpackforlaravel.com/docs/6.x/crud-operation-list-entries#the-search-logic-1 I came across this
An example as to how to do this would be really helpful!! (Rather than having to go hunting through the backpack source)
Currently, I'm trying to debug why my ListOperation isn't using the correct searchLogic the website error is returning a totally different query to what is in my searchLogic override and I can't work out why?!!
Just in case it's needed, my List column is:
mysql is returning a
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'clients.lastname' in 'WHERE'
error for the queryI'm kind of hoping that if I put the search logic in its' own function I might be able to work out what's going wrong?!!
Cheers for any help
:)
Beta Was this translation helpful? Give feedback.
All reactions