TC
Twill CMS•9mo ago
HealMan

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
28 Replies
ifox
ifox•9mo 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
HealMan
HealManOP•9mo 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?
ifox
ifox•9mo 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
HealMan
HealManOP•9mo 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?
HealMan
HealManOP•9mo 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
ifox
ifox•9mo 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.
HealMan
HealManOP•9mo ago
Actually I added type and tried. Still shows the error.
No description
ifox
ifox•9mo ago
you forgot the return type : void
HealMan
HealManOP•9mo ago
Oh I don't see that error anymore. But
HealMan
HealManOP•9mo ago
Could you please resolve this one again?
No description
HealMan
HealManOP•9mo ago
I added this code in location controller. protected function formData($request) { return [ 'sectors' => app()->make(TagRepository::class)->listAll() ]; }
ifox
ifox•9mo 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
HealMan
HealManOP•9mo 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'] ?? []); ?
ifox
ifox•9mo 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
HealMan
HealManOP•9mo ago
Well... I'd make the pool name "MyTag" rather than "Tag"?
ifox
ifox•9mo 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.
HealMan
HealManOP•9mo 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
ifox
ifox•9mo 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.
HealMan
HealManOP•9mo 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?
ifox
ifox•9mo ago
Hi! you seem to be still missing the types of the function parameters
HealMan
HealManOP•9mo ago
Hi, thanks for your prompt response. I don't get any php syntax issues right now.
HealMan
HealManOP•9mo ago
When I tried to update when editing the model, it shows this.
No description
ifox
ifox•9mo ago
Ok, can you check the logs to see the error?
HealMan
HealManOP•9mo ago
No description
ifox
ifox•9mo 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)
HealMan
HealManOP•9mo 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?
ifox
ifox•9mo ago
Same subject would be in this thread. If it's about something else, please post a separate message (thread is automatically created)
HealMan
HealManOP•9mo ago
Okay, Thanks...
Want results from more Discord servers?
Add your server