Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the rule to create a polymorphic field name in Laravel?

I'm not new using Laravel but it's first time that I need/want to use polymorphic relations. I saw a example at Laravel.com and it says that I should use 'imageable_id' to a table called 'images', in another example I saw 'commentable_id' to table 'comments'.

Is there a rule to create names?

For example: I want to create a table 'category' that will be used for 'posts' and 'settings'. In this case, should I use 'categoryable_id' as name?

like image 679
Marques Avatar asked Sep 22 '15 19:09

Marques


People also ask

What is polymorphic in laravel?

A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one association. A typical example of this is featured images on a post and an avatar for a user. The only thing that changes however is how we get the associated model by using morphOne instead.

What is a polymorphic relationship in SQL?

Polymorphic relationships work by using a lookup table with an additional column specifying which table the foreign key should reference. In non-polymorphic relationships, the foreign keys references a primary ID in a specific table.

What is polymorphic database?

Polymorphic association is a term used in discussions of Object-Relational Mapping with respect to the problem of representing in the relational database domain, a relationship from one class to multiple classes. In statically typed languages such as Java these multiple classes are subclasses of the same superclass.

What kind of relationship is polymorphism?

A polymorphic relationship is where a model can belong to more than one other model on a single association. To clarify this, let's create an imaginary situation where we have a Topic and a Post model. Users can leave comments on both topics and posts.


1 Answers

There is no rule, except a default convention. By default, you define the prefix and Laravel will set the _id and _type suffixes. The field is determined by what you define in the relationship:

For example, on either Posts or Settings:

public function category()
{
    return $this->morphMany('App\Category', 'categoryable');
}

With this, your model will search the table Category for categoryable_id and categoryable_type. categoryable_id by default will match your model id and categoryable_type will match your model's FQCN (e.g. 'App\Post').

If you wish to change how Laravel sets the polymorphic column names, you can set those with the 3rd and 4th parameters of the same method, e.g.:

public function category()
{
    return $this->morphMany('App\Category', 'categoryable', 'model_type', 'model_id');
}

So there isn't really a rule. You are free to define it as you wish, or utilize the default convention.

Source: http://laravel.com/docs/5.1/eloquent-relationships#many-to-many-polymorphic-relations

like image 81
robarelli Avatar answered Nov 05 '22 14:11

robarelli