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 ?
Solution:
Check this, it's working for many-to-many
Jump to solution
22 Replies
daisy21
daisy2110mo ago
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.
daisy21
daisy2110mo ago
the table in the demo should be shop_category_product
einnlleinhatt_
einnlleinhatt_10mo ago
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()
daisy21
daisy2110mo ago
can you show your filament form code?
cheesegrits
cheesegrits10mo ago
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?
einnlleinhatt_
einnlleinhatt_10mo ago
projects table
uuid('id')->primary()
string('name')
string('image')
string('url')
long text('description')
timestamp()

tags table
uuid('id')->primary()
string('name')
timestamp()

project_tag table
primary(['projects_id', 'tags_id'])
foreignUuid('projects_id')
foreignUuid('tags_id')
timestamp()
projects table
uuid('id')->primary()
string('name')
string('image')
string('url')
long text('description')
timestamp()

tags table
uuid('id')->primary()
string('name')
timestamp()

project_tag table
primary(['projects_id', 'tags_id'])
foreignUuid('projects_id')
foreignUuid('tags_id')
timestamp()
ProjectModel

public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class, 'project_tag', 'projects_id', 'tags_id')->withTimestamps();
}

TagModal
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class, 'project_tag', 'projects_id', 'tags_id')->withTimestamps();
}
ProjectModel

public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class, 'project_tag', 'projects_id', 'tags_id')->withTimestamps();
}

TagModal
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class, 'project_tag', 'projects_id', 'tags_id')->withTimestamps();
}
It's an admin panel. It will not work untill I add a new table foreignUuid('tag_id).
cheesegrits
cheesegrits10mo ago
Need to see the form schema, specifically the Select or Tags field.
einnlleinhatt_
einnlleinhatt_10mo ago
Forms\Components\Select::make('tags')
->relationship('tags', 'name')
->multiple()
->required(),
Forms\Components\Select::make('tags')
->relationship('tags', 'name')
->multiple()
->required(),
Vp
Vp10mo ago
Base on your tables and your code it should work.. but I've never seen primary(['projects_id', 'tags_id']) like this..
einnlleinhatt_
einnlleinhatt_10mo ago
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.
einnlleinhatt_
einnlleinhatt_10mo ago
I add this to my projects table and it's work $table->foreignUuid('tags_id')->nullable()->constrained()->nullOnDelete();
Vp
Vp10mo ago
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
einnlleinhatt_
einnlleinhatt_10mo ago
As to my goal I'm correct to use many to many relationship right ?
Solution
Vp
Vp10mo ago
Check this, it's working for many-to-many
Vp
Vp10mo ago
Yes
einnlleinhatt_
einnlleinhatt_10mo ago
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.
Vp
Vp10mo ago
Uuid should not be the problem..
daisy21
daisy2110mo ago
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
einnlleinhatt_
einnlleinhatt_10mo ago
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.
einnlleinhatt_
einnlleinhatt_10mo ago
Thank you, fixed now
daisy21
daisy2110mo ago
can you share the fix?