F
Filamentβ€’12mo 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β€’12mo ago
You need to pass an array: default([2,3,4,10]) Noting default only works on creation not edit.
Chriis
ChriisOPβ€’12mo 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β€’12mo ago
This is on a creation right? And when you remove dd you replace it with return ?
Chriis
ChriisOPβ€’12mo ago
Yep
toeknee
toekneeβ€’12mo ago
You have't fetchted the id's it should return an array of id's.
Chriis
ChriisOPβ€’12mo 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β€’12mo ago
so that DD should return: [single_id]
Chriis
ChriisOPβ€’12mo ago
Yep
toeknee
toekneeβ€’12mo ago
As an example to test the logic, if you replace: ->default([3]) does it work?
Chriis
ChriisOPβ€’12mo 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
ChriisOPβ€’12mo ago
To give you more context, here is the options I have from the relationship on the select
No description
Chriis
ChriisOPβ€’12mo 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
ChriisOPβ€’12mo 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β€’12mo 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
ChriisOPβ€’12mo ago
Hmm ... πŸ˜… It works ofc I don't know why I didn't think of that
toeknee
toekneeβ€’12mo ago
haha
Chriis
ChriisOPβ€’12mo ago
I was doing some weird thing with pluck toArray etc πŸ˜‘
toeknee
toekneeβ€’12mo ago
because you were too fixated on the query πŸ˜‰ programmers tunnel vision, it's why asking questions or going for a walk helps πŸ˜›
Chriis
ChriisOPβ€’12mo ago
Yep, thanks a lot πŸ™‚
Want results from more Discord servers?
Add your server