adding "second order" relationships to a CRUD's setupListOperation()? #1269
-
Let's say I have two tables - a users table and a dues table. The dues table has a userID column which is a foreign key to the primary key of the users table (usersID). A So like with raw SQL it'd look like this: SELECT u.name, u2.name
FROM dues d
JOIN users u ON d.userID = u.userID
LEFT JOIN users u2 ON u.spouseID = u2.userID So like you'd need to do two JOINs from the main table to get that data to show up and the last JOIN would need to be a LEFT JOIN. My question is... how would I do this in a CRUD, specifically in the "First order" relationships can be defined thusly: 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.'%');
});
}
]); But that approach doesn't work for "second order" relationships. I guess an SQL VIEW would take care of this but I'd prefer to do this in PHP / Laravel if it at all possible! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To add a "second order" relationship in the CRUD::column([
'label' => 'Spouse',
'name' => 'payer.spouse.name',
'type' => 'relationship',
'entity' => 'payer.spouse',
'attribute' => 'name',
'model' => 'App\Models\User',
'searchLogic' => function ($query, $column, $searchTerm) {
$query->orWhereHas('payer.spouse', function ($q) use ($searchTerm) {
$q->where('name', 'like', '%'.$searchTerm.'%');
});
}
]); This approach uses the dot notation to access the spouse relationship through the payer relationship. The For the search functionality, we use It's important to note that this method may cause performance issues if you're dealing with a large number of records, as it will result in additional database queries. In such cases, you might want to consider eager loading the relationships or using a database view as you mentioned. If you encounter any issues with this approach or need to optimize it further, you might want to consider using a custom column type or modifying the query in the Backpack Documentation provides more information on working with relationship columns in Backpack CRUD. ℹ️ 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 add a "second order" relationship in the
setupListOperation()
method of your CRUD controller, you can use dot notation to access nested relationships. Here's how you can modify your code to display the spouse's name:This approach uses the dot notation to access the spouse …