Multi-Select Relationship by Image / Visually

I have a model: Exhibition which can have many Models: Artwork
How can I give the same same experience/results as a mulit-select \Forms\Components\Select() BUT with images rather than a text dropdown. In short I need the user to be able to search for images and select them to assign many artwork images to a single exhibition. Should I but using select? or maybe a RelationManager is better? Would love some advice please My latest attempt is with a relation-manager but that attempts to create new artwork which I don't want...I need to assign many artworks, visually to a parent exhibition.
22 Replies
LeandroFerreira
Filament
Render HTML in select options by Matthew Ost - Tricks - Filament
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
bionary
bionaryOP2y ago
hmmm, trying that out now I was going okay...but then when using multi-select hooked up to a reactive select I get some weirdness. Still pursuing, but currently when the select window looses focus, all of the previously selected items lose their styling and revert to text IDs. Sadly I cannot get this to work with Select()->multiple() based on a parent Select()->reactive() field When the reactive select is changed...all of the previously selected images in the select box turn to ID numbers...losing their styling. I though maybe I could retain the formatted state of the Select input by storing the IDs in a static property outside of the form() and simply merge the IDs and re-query them to always return their data, but it's a big fat miss.
awcodes
awcodes2y ago
Is you multiple select a relationship.?
bionary
bionaryOP2y ago
I wonder if this could be handled with a relation-manager? But a relation-manager is more for creating new but related records. I am only interested in building relationhips but NOT creating anything new Yes, ->multiple() is a necessity
awcodes
awcodes2y ago
Better question what is the code for the related select?
bionary
bionaryOP2y ago
Let me pull out all of my static var nonsense or your head will explode...give me a minute
Kenneth Sese
Kenneth Sese2y ago
Make sure you are using getOptionLabelsUsing and not getOptionLabelUsing. The "s" at the end matters when using multiple
bionary
bionaryOP2y ago
oh dang, let me see if there is magic in that "s" @kennethsese nope, that made the whole thing error out when changing the reactive() select. Here is what I have so far:
Select::make('selected_artist')
->searchable()
->reactive()
->options( function(Closure $get){
return
User::whereHas('roles', function($query) {
$query->where('name', 'artist');
})
->orderBy('name')
->get()
->pluck('name', 'id');
}),

Select::make('art_ids')
->label('Selected Artworks')
->multiple()
->allowHtml()
->searchable()
->options(function (Closure $get) {
if($get('selected_artist')) {
$artworks = Art::where('dash_user_id', $get('selected_artist'))->get();
}else{
$artworks = Art::get();
}
return $artworks->mapWithKeys(function ($artwork) {
return [$artwork->getKey() => static::getCleanOptionString($artwork)];
})->toArray();
})
->getSearchResultsUsing(function (Closure $get, string $search) {
$artworks = Art::where('dash_user_id', $get('selected_artist'))->where('title', 'like', "%{$search}%")->limit(50)->get();
return $artworks->mapWithKeys(function ($artwork) {
return [$artwork->getKey() => static::getCleanOptionString($artwork)];
})->toArray();
})
->getOptionLabelUsing(function ($value): string {
$artwork = Art::find($value);
return static::getCleanOptionString($artwork);
})
Select::make('selected_artist')
->searchable()
->reactive()
->options( function(Closure $get){
return
User::whereHas('roles', function($query) {
$query->where('name', 'artist');
})
->orderBy('name')
->get()
->pluck('name', 'id');
}),

Select::make('art_ids')
->label('Selected Artworks')
->multiple()
->allowHtml()
->searchable()
->options(function (Closure $get) {
if($get('selected_artist')) {
$artworks = Art::where('dash_user_id', $get('selected_artist'))->get();
}else{
$artworks = Art::get();
}
return $artworks->mapWithKeys(function ($artwork) {
return [$artwork->getKey() => static::getCleanOptionString($artwork)];
})->toArray();
})
->getSearchResultsUsing(function (Closure $get, string $search) {
$artworks = Art::where('dash_user_id', $get('selected_artist'))->where('title', 'like', "%{$search}%")->limit(50)->get();
return $artworks->mapWithKeys(function ($artwork) {
return [$artwork->getKey() => static::getCleanOptionString($artwork)];
})->toArray();
})
->getOptionLabelUsing(function ($value): string {
$artwork = Art::find($value);
return static::getCleanOptionString($artwork);
})
Kenneth Sese
Kenneth Sese2y ago
When using that you need to use $values which is an array so like this (adapt as necesary): ->getOptionLabelsUsing(fn ($values): array => YourModel::find($values)?->pluck('name', 'id')
bionary
bionaryOP2y ago
@kennethsese okay I see, thanks. Now what happens is the entire Select field resets and clears out (blank) after the parent/reactive select is changed. Maybe I can try using that static property holding the collection again
Kenneth Sese
Kenneth Sese2y ago
Won't your child options change when your parent changes?
bionary
bionaryOP2y ago
Good question? This is a multiselect so i was hoping they dont
Kenneth Sese
Kenneth Sese2y ago
So you want to pick a Parent A and Child A1 and A2 and then be able to swtich to Parent B and then choose B1 and B2, but also keep A1 and A2? Maybe this is a many to many?
bionary
bionaryOP2y ago
I actually set up a pivot table originally and am not opposed to that for sure. The reason I moved away from the pivot table idea was because the relation manager seems to only allow for creating...not asigning but I am wide open for ideas!
Kenneth Sese
Kenneth Sese2y ago
You can totally attach relationships using a relation manager. You can create too, but also attach/detach
bionary
bionaryOP2y ago
I just need a way to 1) select > artist 2) select artworks associated with that artist 3) attach to the parent model: Exhibition and it needs to be visually based cannot be text only
bionary
bionaryOP2y ago
yeah...im into it hot damn...when you think you've seen it all These fandangled Filamentors have done gone thought of everything - lol I was barking way to loudly up the wrong tree
Kenneth Sese
Kenneth Sese2y ago
You should be able to customize the RM attach select to make it visual as well. That's not to say you couldn't keep going down your select field path...you probably could. I think it just depends on how you want it to function. But a RM is probably going to be a lot eaiser
bionary
bionaryOP2y ago
I like the RM path and it's the one I originally planned on taking because then the artwork can be sorted or clicked through for further actions The docs mention that you must use ->withPivot() is that deprecated advice?
awcodes
awcodes2y ago
No. WithPivot is not deprecated. It forces your relationship to append the pivot data onto the record with a join. Making it accessible as attributes on the model.
bionary
bionaryOP2y ago
Yes! The pivot table + relation-manager was indeed the way to go...I added integrated the select/html code (from above) and it works like magic. The second select simply becomes $action->getRecordSelect() . thanks guys
Want results from more Discord servers?
Add your server