How to customize relationship pivot field orderBy
See original GitHub issueBug report
What I did
Added a custom query in a relationship field to order the options by name.
CRUD::addField([
'label' => "Catégorie",
'type' => "relationship",
'name' => 'type_id',
'attribute' => "name",
'model' => "App\Models\Type",
'placeholder' => "Sélectionner une catégorie",
'inline_create' => true,
'tab' => 'Champs d\'administration',
'options' => (function ($query) {
return $query->orderBy('name', 'ASC')->get();
})
]);
What I expected to happen
The select2 options should be ordered by name.
What happened
The select2 options are not ordered by name.
What I’ve already tried to fix it
If I remove 'inline_create' => true
, the custom query is working as expected and the select2 options are ordered by name.
CRUD::addField([
'label' => "Catégorie",
'type' => "relationship",
'name' => 'type_id',
'attribute' => "name",
'model' => "App\Models\Type",
'placeholder' => "Sélectionner une catégorie",
'tab' => 'Champs d\'administration',
'options' => (function ($query) {
return $query->orderBy('name', 'ASC')->get();
})
]);
Backpack, Laravel, PHP, DB version
PHP VERSION: PHP 8.0.17 (cli) (built: Mar 29 2022 02:31:00) ( NTS ) Copyright © The PHP Group Zend Engine v4.0.17, Copyright © Zend Technologies
LARAVEL VERSION: v8.83.2@b91b3b5b39fbbdc763746f5714e08d50a4dd7857
BACKPACK VERSION: 5.0.13@d78f4263f5eef1e7761ba2d55d81959f4de559b6
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
laravel - How to sort by a field of the pivot table of a many-to ...
I have been doing this ( on several builds ) simply using the relationship method as you have. I often use an 'order'...
Read more >Sorting to your Pivot table row labels in custom order [quick tip]
Drag and drop the row labels to re-arrange them. Pivot table will remember this order even when you refresh. Of course there is...
Read more >Sort data in a PivotTable or PivotChart - Microsoft Support
Set custom sort options · Click a field in the row or column you want to sort. · Click the arrow Filter drop-down...
Read more >Order by pivot table field - Laracasts
Hi there! Couldn't find an answer for this one, even after longer searching: I want to order a list of items by the...
Read more >Pivot Table: Sort row by hidden field - microsoft excel
Hide the "Sum of Sort" column in Excel. · Don't touch the "Row Labels" dropdown again (if you want, hide it via the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I tried it and it works. @pxpm Thanks Post the correct code in case someone needs it:
@pxpm Thanks for the tip! It’s working as expected now that I’ve added the orderBy in the fetch function.