Multi-Select from Dynamic Values

I created Location Model. And each location has multiple tags. I want to share tags across different locations, I mean I want to use tag pool which location model can use. I followed https://twillcms.com/docs/form-fields/multi-select.html#content-multi-select-with-dynamic-values, but it didn't work. Looking for help on this. Thanks
Twill
I
ifox50d ago
The tags field behaves exactly like what you describe. Tags are namespaced by model. So any tag created in a location will be available in other locations, but not in the tags field of other models
H
HealMan50d ago
Well.... Thanks for your answer. But I want to share tag pool in different models too. Anyhow, I see no option when using tag field. If I add new tag in one location, is that tag shared in location model? And how can I add tags without adding new tag in location form editor?
I
ifox50d ago
ok, if you want that, definitely makes sense to create your own tags module The thing is in that case you won't be able to create new tags from other models, you'll have to create tags upfront. We will be working soon on the ability to create things inline in select and browser fields. So in the example in the docs, sectors would be your tags
H
HealMan50d ago
Yeah... I followed the steps mentioned by the link. And got some problems and couldn't understand entirely. Could you please explain more details about last 3 steps? afterSave, formData?
H
HealMan50d ago
Actually I wrote this code into my location repository. public function afterSave($object, $fields) { $object->sectors()->sync($fields['sectors'] ?? []);
parent::afterSave($object, $fields); } } but it shows this error.
No description
I
ifox50d ago
This is a PHP function parameters types error, the response is in the error message, you need to add types to the parameters of that function. We'll update the docs to include these.
H
HealMan50d ago
Actually I added type and tried. Still shows the error.
No description
I
ifox50d ago
you forgot the return type : void
H
HealMan50d ago
Oh I don't see that error anymore. But
H
HealMan50d ago
Could you please resolve this one again?
No description
H
HealMan50d ago
I added this code in location controller. protected function formData($request) { return [ 'sectors' => app()->make(TagRepository::class)->listAll() ]; }
I
ifox50d ago
you don't need that with twill 3 form builder since you are passing a sectors variable yourself, so you can use that code in place of the sectors variable in your first screenshot
H
HealMan50d ago
well... you mean I don't need options()? and since the name of field is 'tags' so should it be $object->sectors()->sync($fields['tags'] ?? []); ?
I
ifox50d ago
You do, but you don't need formData
->options(Options::fromArray(app()->make(TagRepository::class)->listAll()))
->options(Options::fromArray(app()->make(TagRepository::class)->listAll()))
why would you use sectors? That's the example model name in the docs, but you're not working with sectors, you're working with tags You're going to run into an issue because the tags() relationship already exists on Twill models, for the tags field, so you should call your relation in your model differently. But seeing that you use sectors in your repository code, I'm pretty sure you misconfigured the model with a sectors relationship too. Step back a little bit and don't blindly copy and paste code without understanding what you're trying to do
H
HealMan50d ago
Well... I'd make the pool name "MyTag" rather than "Tag"?
I
ifox50d ago
Your model can still be called Tag, MyTag isn't pretty haha. Laravel allows you to use any relationship name, it's just that if your model and relationship are not called the same, you have to be explicit about it. Now, let's step back. You said you want to be able to associate tags with multiple models, not just Location, correct? The type of relationship in the documentation you are referencing is not the best for that use case, because you would need to create a pivot table for each of your model that use your tags. I would recommend you use a browser field instead, it will be a lot simpler to setup for you as there is no relationship or custom code needed.
H
HealMan50d ago
Oh, that is really helpful. Actually, I am not sure we are gonna use tag pool in different models but currently we have one location model. And I will try again with the documentation. Well... so If I select one or more options on the location, the records are saved in pivot table? Well... what I am going to say is that before when I see the errors including type errors.. I just skipped that step. and used this one only. $form->add( MultiSelect::make() ->searchable() ->name('tags') ->label('Tags') ->options(app()->make(TagRepository::class)->listAll()->toArray()) ); After I selected one or more options from location and updated. there was no record in pivot table. And also there is no data related to the selected options in locations table too. But I still see the selected options in next edit. I guess, this is related to the built in tag model and relation? I know this doesn't make sense at all and sorry for taking too much of your time. but please help me again and let me know your thoughts on it. 🥺 It seems like you are busy, I am still waiting for your answers fingers crossed. But no problem, just text here when you are convenient. @ifox
I
ifox50d ago
Please don't tag me to get an answer faster. I'll reply when I reply. This is a free open source community, so you can't just expect me to respond when you need it immediately. It is not saving pivot data because you haven't set it up correctly. Can you share your repository privately? It will be quicker to help you directly in the code rather than try to fix from screenshots.
H
HealMan47d ago
Thank you, Will share after trying once again. Have a great weekend. Hi, @ifox , Good Morning, Hope you had a great weekend. public function afterSave($object, $fields) : void { $object->labels()->sync($fields['labels'] ?? []);
parent::afterSave($object, $fields); } I created label module and all are working fine. But above code snippet is not working. If I use this, i can't update the model. If I remove this, all is working fine. Do you have any thoughts on it?
I
ifox47d ago
Hi! you seem to be still missing the types of the function parameters
H
HealMan47d ago
Hi, thanks for your prompt response. I don't get any php syntax issues right now.
H
HealMan47d ago
When I tried to update when editing the model, it shows this.
No description
I
ifox47d ago
Ok, can you check the logs to see the error?
H
HealMan47d ago
No description
I
ifox47d ago
ha, interesting, the position column is not needed, so our docs are wrong. try to remove it from the migration (or in a new migration if you already deployed the create migration)
H
HealMan47d ago
Yeah, Perfect! It worked now!!! Thank you so much. If I need more help, can I use this thread or create a new one?
I
ifox47d ago
Same subject would be in this thread. If it's about something else, please post a separate message (thread is automatically created)
H
HealMan47d ago
Okay, Thanks...
Want results from more Discord servers?
Add your server
More Posts
How should I migrate from Legacy Settings to V3 Settings?I am having trouble accessing the setting goroup I created. When do php artisan route:list, settingCannot use connectedTo: Cannot unpack array with string keysHi I am having issues getting connectedTo to work in my Block. I have Twill 3.1.0 installed, but whasynchronous request in selectGood afternoon, I have two select fields. The thing is that when selecting the first select field, IUpgraded from Twill 2.13 to 3.2, frontend error when saving a module that uses 'editInModal' => trueI upgraded from Twill 2.13 to 3.2. When saving a module that is created/edited via a modal (using $iUpgraded from Twill 2.13 to 3.2, raw HTML links visible in browserHi, I upgraded from Twill 2.13 to 3.2. I am now seeing raw HTML edit links in my "Projecttags" browHow to use Conditional Fields in ControllerI want to add a Author select fields when shared_by === user ` $form->add( Radios::maBlock editor preview Vite issue?Hey all, I'm getting this when I preview a page module block editor in the admin:Upgrading from 2.0 to 3.0 -Block Editor $groups issueHello! I'm getting a strange error (see picture). I've tried clearing all caches. It only happens onInline Repeater get's overridden by another InlineRepeater with the same name in another block.The code in first image get's overriden by the code in the second image in admin panel. Normally, whTable custom columnHello! I created module Product and relationship with module Category. I would like to display categMulti select selected issueIn twill3 I created 2 CRUD, ImportedItem and ItemTypes. I used Multiselect for selecting ItemTypes. Upgrading from 2.0 to 3.0 - routing issuesHello! I'm currently updating twill to 3.0 from 2.0, and I'm having some issues with routes. I'm getDisabled defaults in controller based on user RoleGroupI want to disable, create, edit or delete actions according to user RoleGroup or there is another waDrag and drop issueHello, any idea how to achive DnD working on modules list. Problem is in that when you go below limiFile library 404 errorHello, I'm encountering issues with the file library. When I upload a file, such as a .txt file, it'InlineRepeater deleting data on related Model entriesI'm using an InlineRepeater to add relations with pivot data between two Repositories. The relations"astrotomic/laravel-translatable" n+1 queriesI've got a package issue which is composer required by twill. 800+ queries on a model only getting Repeaters and Blockeditor won't work with media library.I saw the pst regarding JSON repeaters. So I switched to the block editor for the content that NEEDSTag Field on Block not Displaying Saved TagsHello. I am using the Tag Field in a block, the tags are being saved correctly, but the field is cFile link in view issueHi everyone i'm trying to render a file in blade view bug seem difficult to me how can i render lin