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
28 Replies
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
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?
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
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?
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.
parent::afterSave($object, $fields); } } but it shows this error.
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.
Actually I added type and tried.
Still shows the error.
you forgot the return type
: void
Oh I don't see that error anymore. But
Could you please resolve this one again?
I added this code in location controller.
protected function formData($request)
{
return [
'sectors' => app()->make(TagRepository::class)->listAll()
];
}
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
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'] ?? []); ?
You do, but you don't need formData
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 doWell... I'd make the pool name "MyTag" rather than "Tag"?
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.
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
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.
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?
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?
Hi! you seem to be still missing the types of the function parameters
Hi, thanks for your prompt response.
I don't get any php syntax issues right now.
When I tried to update when editing the model, it shows this.
Ok, can you check the logs to see the error?
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)
Yeah, Perfect! It worked now!!!
Thank you so much.
If I need more help, can I use this thread or create a new one?
Same subject would be in this thread. If it's about something else, please post a separate message (thread is automatically created)
Okay, Thanks...