Text Version of the Lesson
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.
Option 1. Just Attach: Select Multiple
Let's add one more field in the form of...
This section seemed to be a little less considered around creating the pivot table and the requirement to include
use Illuminate\Database\Eloquent\Relations\BelongsToMany;. I worked around these and eventually got it working but just noting it since there appears to be some missing connections in this lesson. Great course though.That is true, and I do like (and miss) some of the hand-holding sometimes, but Povilas did mention that the pivot tables would not be his emphasis topic of this lesson. Probably many ways to do it, but I just added the migration:
php artisan make:migration create_product_tagthen added the middle columns like so (there are other ways):
Schema::create('product_tag', function (Blueprint $table) { $table->id(); $table->foreignId('product_id'); $table->foreignId('tag_id'); $table->timestamps(); });Add the relationships in the models (in Product.php add):
public function tags(): BelongsToMany { return $this->belongsToMany(Tag::class); }add the inverse in Tag.php :
public function products(): BelongsToMany { return $this->belongsToMany(Product::class); }Some sample data in the intermediary (pivot) table, and it worked fine.
Thanks @johnogle to provide this instruction.
@PBTraining, I've just added the "use BelongsToMany" line of code. But, as Johnogle said, in this course I'm not hand-holding for the Laravel part, as Filament is a tool ON TOP of Laravel, which implies that you already have the relationships/tables set up. The course is already quite long even without the Laravel part, and if something is not working for you, feel free to ask questions in the comments.
As always - great tutorial Povilas, thank you. And i like the text format, i think for this type of tutorials it's the best.
As for Laravel part, I would also prefer at least to see a mention of required pivot table and similar things. It's not too many extra lines of text and code, but would totally make it clear what we're working with.
I think that instead of literal hand-holding, it would be better just to give instructions as a list on what is required to move forward. So, something like:
As a bonus, it'd be nice for there to be links either to other courses or to the Laravel docs so that people can go off and fill in gaps / refresh their knowledge and then continue.
The only thing I found a bit disruptive to learning was the fact that I wasn't specifically told what I needed to do to prepare for the lesson. So then part way through, I was kinda thinking, "hmm.. I'm going to need a pivot table here"
I definitely agree with the idea of not adding lots of extra details about how to do this though as it's out of scope. But a few signposts would be nice I think :-)