Skip to main content

BelongsTo, BelongsToMany or Polymorphic? Three Examples.

Lesson 01/20 8:02
Autoplay

Comments & Discussion

F
fpolli ✓ Link copied!

The Tags relation seems to be the same as the Photos in that you could add more content types that would have tags (podcast, book, etc), and polymorphic would remove the need to modify the model (consistent with the open-closed principle from your other course :). After watching this I read the Laravel docs on polymorphic many-to-many and it seemed clear enough.

SV
Siyasat Vahidoğlu ✓ Link copied!

Thanks))

TG
Thananjeyan G ✓ Link copied!

Hi Povilas,

I have doubts on creating relationship for this situation.

User has a role ( Front team, Backend team, Manager , Admin )

If user is Front or backend, then they should have any Manager,

i created table like structure Manager_relation user_id, manager_id (userid), type (backend / frontend)

What kind of this relation this ? Is there any other betterway to handlet this ?

Thank you.

PK
Povilas Korop ✓ Link copied!

There's no easy and quick way to answer this, it depends on what other operations you will perform with those users, areas and managers. In your case, it's a pivot table with additional column, which looks fine.

BO
Bruno Oliveira ✓ Link copied!

Hi Povilas, you mentioned that there is a rule of naming convention in laravel. I've found some of them on laravel.com but, is there any document with all these rules? I'm really interested in getting a better understanding of these rules, especially those related to variables and model properties.

Thank you.

M
Modestas ✓ Link copied!

No, we have not seen a good document with the naming conventions in them. They are just there, but as always - you can go and create whatever you want yourself :)

RA
Richard A. Hoyle ✓ Link copied!

I created a pivot table that I am not sour I need The tables are Address Type and Address the addresstype only has one filed name and the Address has the address_type_id, street_address,apt_suit_lot, city,state, and postal_code. Each address has only one type but the addresses hasMany addresstypes In the pivot table

Schema::create('address_address_type', function (Blueprint $table) {
$table->foreignIdFor(Address::class)->constrained();
$table->foreignIdFor(AddressType::class)->constrained();
});

I had to manually put each of the records in to the table Not sour why however This is my first time actually using a pivot table on my own so one of my questions is how to get the pivot table to automatically fill with the data? Besides the Question do I even need it?

RA
Richard A. Hoyle ✓ Link copied!
Schema::create('address_address_type', function (Blueprint $table) {
$table->foreignIdFor(Address::class)->constrained();
$table->foreignIdFor(AddressType::class)->constrained();
});

Changed the table to

Schema::create('address_address_type', function (Blueprint $table) {
$table->foreignId('address_id')->constrained();
$table->foreignId('address_type_id')->constrained();
});

but it still is not getting filled

M
Modestas ✓ Link copied!

Pivot tables fill automatically if you create the record using pivot relationship (many to many). It's impossible to tell what exactly is wrong here since it is just a database migration code.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.