Video Version of the Lesson
[Only for premium members]
[Only for premium members]
[Only for premium members]
Now, let's see how we can add data to a many-to-many relationship. Let's say that our Product may have many Tags.
We generate the Model+Migration:
php artisan make:model Tag -m
Here's the structure, with relationship right away:
Migration
Schema::create('tags', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps();});
app/Models/Tag.php:
use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Tag extends Model{ protected $fillable = ['name']; public function products(): BelongsToMany { return $this->belongsToMany(Product::class); }}
Also, the relationship from the other way:
app/Models/Product.php:
use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Product extends Model{ // ... public function tags(): BelongsToMany { return $this->belongsToMany(Tag::class); }}
Next, we generate this simple Filament Resource:
php artisan make:filament-resource Tag --simple --generate
The result is this:
But, of course, the Tags CRUD is not the topic of this lesson. What we need to do is add tags to products. Multiple tags.
There are a few ways to do it.
Let's add one more field in the form of...