How to order record of relationship fields? #1251
Replies: 2 comments 1 reply
-
To order records in a relationship field in Backpack, you can use the CRUD::field([
'name' => 'paths',
'label' => __('backend/museum.info.paths'),
'type' => 'relationship',
'entity' => 'paths',
'attribute' => 'name',
'pivot' => true,
'multiple' => true,
'model' => 'App\Models\MuseumPath',
'inline_create' => ['entity' => 'museum-path'],
'wrapper' => [
'class' => 'form-group col-md-12'
],
'subfields' => [],
'pivotSelect' => [
'label' => __('backend/museum.paths.select_path'),
'inline_create' => [
'entity' => 'museum-path',
'modal_route' => route('museum-path-inline-create'),
],
'data_source' => route('museum.fetchMuseumPaths'),
],
'tab' => $paths_tab,
'orderColumn' => 'order', // Add this line
]); By adding the Here are some important points to note:
If you want more control over the ordering, you can also use the following syntax: 'orderColumn' => ['name' => 'order', 'type' => 'number', 'attributes' => ['data-reorder-input' => true]] This will add a visible number input field for the order, in addition to the up and down arrows. Remember to update your database schema and model relationships accordingly to support this new ordering functionality. ℹ️ 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: Github Issue (closed): How to customize relationship pivot field orderBy |
Beta Was this translation helpful? Give feedback.
-
Hey @cod3rshotout The bot is partially correct. Follow these steps to reorder the
public function owners()
{
return $this->belongsToMany(\App\Models\PetShop\Owner::class)->withPivot(['role', 'order']);
}
CRUD::field('owners')
->subfields([['name' => 'role', 'type' => 'text']])
+ ->reorder('order'); This will add up and down arrows next to each row in the relationship field, allowing you to reorder the items. You can also check a diff here of a branch I used to test before answering. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have created a relationship field and I need a way to reoder items like a repeater:
how can I do that?
Beta Was this translation helpful? Give feedback.
All reactions