Relationship belongstoMany
Good evening, I tried to make my own portfolio using filament since I'm not good at frontend.
My goal is a project can have many tags and a tags can have many project too. For example
Project A Tag1, Tag2, Tag3
Project B Tag2, Tag3
Project C Tag3
I followed the filament code source on GitHub for the database migration. On the filament demo, a product can have multiple categories. So I check the database migration for tables product, there is no category_id in it but I can create a product with multiple categories.
In my case I need to add foreignUuid for tag_id inside my projects table.
Anyone can explain to me ?
22 Replies
there is no category_id inside product tables, because if u are using manytomany it will create a new table pivot table i suggest you read this https://laravel.com/docs/10.x/eloquent-relationships#many-to-many
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
the table in the demo should be
shop_category_product
Yeah, I did follow the source code but when I tried to create a new project and submit. It will give me an error like tag_id is column is not exist or something like that.
I have 3 tables
projects
tags
project_tag
I'm not sure if because I'm using uuid('id)->primary()
can you show your filament form code?
Show the code you have for the tags() relationship on your Project model, and your Project form scheme.
Is this in an admin panel, or standalone form?
It's an admin panel.
It will not work untill I add a new table foreignUuid('tag_id).
Need to see the form schema, specifically the Select or Tags field.
Base on your tables and your code it should work.. but I've never seen
primary(['projects_id', 'tags_id'])
like this..It won't work untill I add tag_id column inside projects table. I'm following filament source code in GitHub.
https://github.com/filamentphp/demo/tree/main/database/migrations
The shop products, categories and category product migration.
GitHub
demo/database/migrations at main · filamentphp/demo
Source code for the demo.filamentphp.com website. Contribute to filamentphp/demo development by creating an account on GitHub.
I add this to my projects table and it's work
$table->foreignUuid('tags_id')->nullable()->constrained()->nullOnDelete();
If you add tag_id inside project table, then it cannot be many-to-many, I'll share my gits related many-to-many for refs
https://gist.github.com/valpuia/087dad4afc37818a3d532d63767ca430
As to my goal I'm correct to use many to many relationship right ?
Solution
Check this, it's working for many-to-many
Yes
It's not the uuid problem right?
Also thanks for the gits. I will try using id with a new project when I'm home.
Uuid should not be the problem..
wait is this really the error?
It will give me an error like tag_id is column is not exist u said error was tag_id but your table use tags_id
you should screenshot the error too
Yes we have the same error I think, just different name.
My original tables doesn't have tags_id column, but when I add it. It's work.
Thank you, fixed now
can you share the fix?
I'm following this code
https://discord.com/channels/883083792112300104/1156455683986825246/1156487354022887544