DefaultSort on a Repeater

I've been searching high and low, but couldn't find an answer to this. Is there a way to decide the sorting order for items in a repeater? This is my repeater and its components:
Repeater::make('notes')
->relationship('notes')
->schema([
Forms\Components\Textarea::make('contents')
->label(fn (Get $get) => User::whereId($get('creator'))->first()->full_name . " - Created at: " . Carbon::parse($get('created_at')) ?? 'Add Note')
->maxLength(65535)
->required(),
Forms\Components\Checkbox::make('send_as_email')
->hiddenOn('view')
->label('Send note as email to client?')
->helperText('This sends the contents of the note to the client of this task, as an email.')
->default(false),
])
->mutateRelationshipDataBeforeCreateUsing(function (array $data) {
$data['creator'] = auth()->id();
return $data;
})
->columnSpanFull()
->defaultItems(0),
Repeater::make('notes')
->relationship('notes')
->schema([
Forms\Components\Textarea::make('contents')
->label(fn (Get $get) => User::whereId($get('creator'))->first()->full_name . " - Created at: " . Carbon::parse($get('created_at')) ?? 'Add Note')
->maxLength(65535)
->required(),
Forms\Components\Checkbox::make('send_as_email')
->hiddenOn('view')
->label('Send note as email to client?')
->helperText('This sends the contents of the note to the client of this task, as an email.')
->default(false),
])
->mutateRelationshipDataBeforeCreateUsing(function (array $data) {
$data['creator'] = auth()->id();
return $data;
})
->columnSpanFull()
->defaultItems(0),
I want to sort this by created_at so the newest is at the top; Is there a way I can do this?
Solution:
Something to try - add the orderBy on the relationship definition:
return $this->hasMany(Note::class)->orderBy('created_at', 'desc');
return $this->hasMany(Note::class)->orderBy('created_at', 'desc');
...
Jump to solution
10 Replies
Patrick Boivin
Patrick Boivin14mo ago
Can you share the part of your model that defines the notes relationship?
Sauravisus
SauravisusOP14mo ago
It's a simple HasMany
public function notes()
{
return $this->hasMany(Note::class);
}
public function notes()
{
return $this->hasMany(Note::class);
}
Patrick Boivin
Patrick Boivin14mo ago
And is the Repeater field orderable by the user? Or should it be fixed?
Sauravisus
SauravisusOP14mo ago
It should be fixed, but ordered by created_at so newest is at the bottom.
Patrick Boivin
Patrick Boivin14mo ago
When you add an item in the Repeater, it should show at the bottom by default. Are you creating items also from other places in the app? (Just trying to understand the context)
Sauravisus
SauravisusOP14mo ago
I'm not, but my client wants his notes added to be sorted by newest first, and that's been a real headache 😂
Patrick Boivin
Patrick Boivin14mo ago
Ok, I understand. So the issue is that the new items added in the Repeater don't have a concept of created_at, until the record is actually saved...
Solution
Patrick Boivin
Patrick Boivin14mo ago
Something to try - add the orderBy on the relationship definition:
return $this->hasMany(Note::class)->orderBy('created_at', 'desc');
return $this->hasMany(Note::class)->orderBy('created_at', 'desc');
Patrick Boivin
Patrick Boivin14mo ago
I think the new items will still appear at the bottom, but should be reordered correctly after saving. Not ideal, just something to explore.
Sauravisus
SauravisusOP14mo ago
That worked! Thank you.
Want results from more Discord servers?
Add your server