TP5中模型关联预载入(with)闭包传参方式

  • Post author:
  • Post category:其他


with闭包传参使用use

/**
*注意将$code传入闭包function($query)
*/    
public function get_company(){
        $code = '410400';
        $info = ModelFramework::where([['user_id','=',session('admin_user')['uid']]])->find();
        //查询下属企业
        $company_ids = ModelLiveItem::where([['p_code','like','%'.$info ['id'].'-%']])
            ->with(['company'=>function($query) use ($code){ //外部参数传入
                $query->where([['address_code','like','%'.$code.'%']])->field(['id','name']);
            }])
        ->field('company_id')->group('company_id')->select()->toArray();
        $company_list = array_filter(array_column($company_ids,'company'));
        return $this->success('操作成功','',$company_list);
}



版权声明:本文为qq_34625397原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。