can't search by belongsTo relationship in list operation #1266
-
Here's my protected function setupListOperation()
{
CRUD::column([
'label' => 'Member',
'name' => 'userId',
'entity' => 'payer',
'attribute' => 'name',
'model' => 'App/Models/User'
]);
CRUD::column([
'label' => 'Date Paid',
'name' => 'datePaid'
]);
CRUD::column([
'label' => 'Amount Paid',
'name' => 'amountPaid'
]);
CRUD::column('paypalID')->label('Paid Online')->type('boolean');
} For the "Member" column, the
The Anyway, altho I am able to search by amount paid and date paid I am unable to search by member name. I suppose I could work around this by creating an SQL VIEW where I'm not needing to rely on the Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To address your issue with searching by the belongsTo relationship in the list operation, you can use the Here's how you can modify your protected function setupListOperation()
{
CRUD::column([
'label' => 'Member',
'name' => 'userId',
'entity' => 'payer',
'attribute' => 'name',
'model' => 'App/Models/User',
'searchLogic' => function ($query, $column, $searchTerm) {
$query->orWhereHas('payer', function ($q) use ($searchTerm) {
$q->where('name', 'like', '%'.$searchTerm.'%');
});
}
]);
CRUD::column([
'label' => 'Date Paid',
'name' => 'datePaid'
]);
CRUD::column([
'label' => 'Amount Paid',
'name' => 'amountPaid'
]);
CRUD::column('paypalID')->label('Paid Online')->type('boolean');
} By adding the This solution is based on the information provided in the Backpack CRUD Columns documentation, which explains how to implement custom search logic for columns. With this modification, you should be able to search by member name in your list operation without needing to create an SQL VIEW or modify your database structure. ℹ️ 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: showing relationships in list operation? |
Beta Was this translation helpful? Give feedback.
To address your issue with searching by the belongsTo relationship in the list operation, you can use the
searchLogic
attribute to customize the search behavior for the "Member" column. This allows you to define how Backpack should search for the related User's name.Here's how you can modify your
setupListOperation()
method to enable searching by member name: