question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to customize relationship pivot field orderBy

See original GitHub issue

Bug 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:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
adriallongarriucommented, Jun 9, 2022

You should configure the options inside the pivotSelect key.

I tried it and it works. @pxpm Thanks Post the correct code in case someone needs it:

 $this->crud->addField([
            'name'      => 'products',
            'label'     => 'Products',
            'type'      => "relationship",
            'subfields' => [
                [
                    'name' => 'quantity',
                    'label' => trans('stocks::stock_movment.quantity'),
                    'type' => 'number',
                ],
            ],
            'pivotSelect' => [
                'options'   => (function ($query) {
                    return $query->orderBy('name', 'ASC')->get();
                }),
            ]
        ]);
1reaction
lucaguindanicommented, Jun 3, 2022

The first issue with @lucaguindani , is that the inline_create field does not use options since it’s an ajax field. You should always provide the fetchSomething or configure the data_source yourself. There you actually return the results, so your query configuration should be done in the ajax endpoint: https://backpackforlaravel.com/docs/5.x/crud-operation-fetch#how-to-use-1

@pxpm Thanks for the tip! It’s working as expected now that I’ve added the orderBy in the fetch function.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found