I currently have 3 tables,tags
,product
,album
.
product
andalbum
are all intags
. Distinguish bybiz_type
, 1 meansproduct
, 2 meansalbum
.
The table relationship is as follows
table_tags id: int biz_type: int biz_id: int table_product id: int table_album id: int
I now hope to obtain the information ofproduct
andalbum
by querying the paging list oftags
.
Currently I have checked the polymorphic association oflaravel
, and it seems that this approach is not supported. Is there any way to relate through conditions?
Laravel’s polymorphic many-to-many association talks about this. This usage scenario is somewhat similar to yours. You can take a look
http://www.kancloud.cn/baidu/...
Polymorphic Many To Many Relation Table Structure Polymorphic many to many relational database table structure
In addition to general polymorphic associations, many-to-many polymorphic associations can also be used. For example, the Post and Video models of Blog can share the polymorphic Tag association model. First, let’s take a look at the database table structure:
posts
videos
tags
taggables
Now, we are ready to set up the model association. Both Post and Video models can establish morphToMany associations via the tags method:
class Post extends Model {
}
Create a method for each association in the Tag model:
class Tag extends Model {
}
join? But it is not recommended.
You can also implement a correlation query yourself. Currently, the framework does not provide conditional queries for polymorphic correlations because they are too complex.