Reputation: 909
What is a right naming convention in Laravels when you have 2 morph relations on 1 table (model)?
Example:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
buyers
id - integer
name - string
sellers
id - integer
name - string
comments
id - integer
body - text
commentable1_id - integer // link to video/posts
commentable1_type - string // type link to video/posts
commentable2_id - integer // link to buyers/sellers
commentable2_type - string // type link to buyers/sellers
Now I am using the namme commentable1
and commentable2
, but that is not a very good Laravel way. How can I do this in the right Laravel way?
Upvotes: 1
Views: 620
Reputation: 9693
It will be more about personal preference I guess, if I will be doing, I will do it like
commentable1_id would be video_post_id
commentable1_type would be video_post_type
commentable2_id would be buyer_seller_id
commentable2_type would be buyer_seller_type
And that can be different for another person, but for me, it is self explainable, so I would be using this. Considering you will keep your main model names constant, if you will change that it will change accordingly.
If you want to match laravel standard.
for example your video(s) belongs to post(s), the columns name will be simply videoable_type
and respectively other two columns will be videoable_id
, sellerable_id
and sellerable_type
.
Upvotes: 0