laravel5.4 orm with 用法

在laravel orm 中一个with 关联方法,需要在模板中先定义表与表之间的关系

  /*一对多的关系 */

  public
function hasManyTemplate(){ return $this->hasMany('App\Models\Demo\MakeTemplateFile','user_id','id'); }   /* 一对一的关系 */ public function hasOneVerifyConfig(){ return $this->hasOne('App\Models\Demo\VerifyConfigs','type','verify'); }
            $data = User::with(['hasManyTemplate'=>function($q){
                $q->select(['user_id','preview_address']);
            },'hasOneVerifyConfig'=>function($q){
                $q->select(['type','intro']);
            }])
                ->find(1);
            dd($data);

在使用with的时候 要将关联的字段也加入上,如果不加的话 可能会出现空值(或null)

猜你喜欢

转载自www.cnblogs.com/wjm956/p/8946363.html