thinkphp5 model association


 Requirement: Get the name of the column category in the article list

First associate the 2 models: belongsTo

public function adposition()
    {
        return $this->belongsTo('AdPosition','pos_id', 'pos_id')->field('pos_id,pos_name');
    }

This establishes the association, and then uses the association to preload when fetching the list: with

public function get_list(){
        return $this->with('adposition')->select();
    }


then in the template use

{volist name="list" id="vo"}

{$vo.adposition.pos_name}

{/ volist}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324823997&siteId=291194637