Association object - laravel\lumen How should conditional association be handled?
给我你的怀抱
给我你的怀抱 2017-05-16 16:51:56
0
3
470

I currently have 3 tables,tags,product,album.

The tags associated with

productandalbumare 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 ofproductandalbumby 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?

给我你的怀抱
给我你的怀抱

reply all (3)
洪涛

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

id - integer name - string

videos

id - integer name - string

tags

id - integer name - string

taggables

tag_id - integer taggable_id - integer taggable_type - string

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 {

public function tags() { return $this->morphToMany('App\Tag', 'taggable'); }

}
Create a method for each association in the Tag model:

class Tag extends Model {

public function posts() { return $this->morphedByMany('App\Post', 'taggable'); } public function videos() { return $this->morphedByMany('App\Video', 'taggable'); }

}

    某草草

    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.

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!