F
Filamentβ€’6mo ago
Chriis

How to have a default() for a Select multiple() ?

Hi, I have Select->multiple() and I can't initialize a default value. So what is necessary for inserting a default value ? The id ?
19 Replies
toeknee
toekneeβ€’6mo ago
You need to pass an array: default([2,3,4,10]) Noting default only works on creation not edit.
Chriis
Chriisβ€’6mo ago
Ok, so I don't understand where is my problem 😦
Select::make('specialities')
->default(function () {
dd(Speciality::where('name', Filament::getTenant()->teamSpeciality->name)->first()?->toArray());
})
->relationship('speciality', 'name')
->label('Option')
->multiple()
->nullable()
->preload()
Select::make('specialities')
->default(function () {
dd(Speciality::where('name', Filament::getTenant()->teamSpeciality->name)->first()?->toArray());
})
->relationship('speciality', 'name')
->label('Option')
->multiple()
->nullable()
->preload()
This is the input There is 1 result from this request but I can't make it an array
toeknee
toekneeβ€’6mo ago
This is on a creation right? And when you remove dd you replace it with return ?
Chriis
Chriisβ€’6mo ago
Yep
toeknee
toekneeβ€’6mo ago
You have't fetchted the id's it should return an array of id's.
Chriis
Chriisβ€’6mo ago
And the thing I noticed is that when I dd the result of the Eloquent query I have the 1 id, the one I want to use but when I do
->first()->pluck('id')->toArray()
->first()->pluck('id')->toArray()
I have every record in the DB table
toeknee
toekneeβ€’6mo ago
so that DD should return: [single_id]
Chriis
Chriisβ€’6mo ago
Yep
toeknee
toekneeβ€’6mo ago
As an example to test the logic, if you replace: ->default([3]) does it work?
Chriis
Chriisβ€’6mo ago
Yep, it select the third value of the Select by default So I tried to format the id I have from the Elouqnt query but I don't know why it give me every record in an array as a result when I use pluck It make no sense Like its doing a new query
Chriis
Chriisβ€’6mo ago
To give you more context, here is the options I have from the relationship on the select
No description
Chriis
Chriisβ€’6mo ago
When I do
->default(function () {
dd(Speciality::where('name', Filament::getTenant()->teamSpeciality->name)->first()?->id);
})
->default(function () {
dd(Speciality::where('name', Filament::getTenant()->teamSpeciality->name)->first()?->id);
})
I have as a result
No description
Chriis
Chriisβ€’6mo ago
Which is exactly what I am expecting but this 4 can't be passed in the default like that because its not an array So from this what would you do ?
toeknee
toekneeβ€’6mo ago
That's a single integer you need to cast it to an array so: return [Speciality::where('name', Filament::getTenant()->teamSpeciality->name)->first()?->id];
Chriis
Chriisβ€’6mo ago
Hmm ... πŸ˜… It works ofc I don't know why I didn't think of that
toeknee
toekneeβ€’6mo ago
haha
Chriis
Chriisβ€’6mo ago
I was doing some weird thing with pluck toArray etc πŸ˜‘
toeknee
toekneeβ€’6mo ago
because you were too fixated on the query πŸ˜‰ programmers tunnel vision, it's why asking questions or going for a walk helps πŸ˜›
Chriis
Chriisβ€’6mo ago
Yep, thanks a lot πŸ™‚
Want results from more Discord servers?
Add your server
More Posts