orderLogic breaks CRUD column #1333
-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
The issue you're experiencing with the When you use The solution is to add a 'orderLogic' => function ($query, $column, $columnDirection) {
return $query->leftJoin('users AS u2', 'u2.spouseId', '=', 'users.id')
->orderBy('u2.name', $columnDirection)
->select('users.*'); // Add this line
}, This issue has been documented in several places in the Backpack community. When using joins in This is a known pattern in Backpack as mentioned in this GitHub issue, where a similar problem was solved by adding the Another similar issue was reported in this discussion, where the solution was also to add the explicit select statement to ensure the correct columns are returned. By adding the Let me know if this resolves your issue! ℹ️ 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: orderLogic |
Beta Was this translation helpful? Give feedback.
-
That worked! That said I guess I'll keep this open vs closing it because this kinda does seem like a bug to me. If one of the project maintainers wants to close they're free to do so! |
Beta Was this translation helpful? Give feedback.
-
Hello @prescriptionlifeline, thanks for raising this! I don’t believe this is a bug, because with a left join you end up with two columns named name, and the system may display one that happens to be empty. To ensure the correct data is returned, I recommend making the selection explicit (for example, by aliasing the columns). I’m going to close this issue for now, but please feel free to reopen it or create a new one if you have further questions or run into related problems. Cheers! |
Beta Was this translation helpful? Give feedback.
The issue you're experiencing with the
orderLogic
for your spouse column is a common problem when using joins in Backpack. The problem isn't with your query logic itself, but rather with how Backpack handles the selection of columns when using custom ordering with joins.When you use
orderLogic
with a join, you need to explicitly select the columns from your main table to avoid data being overwritten by the joined table. In your case, when you sort by spouse, the query works correctly, but Backpack isn't properly selecting just the columns from your main users table.The solution is to add a
select('users.*')
to yourorderLogic
function: