In a with association method in laravel orm, the relationship between tables needs to be defined in the template first
/*One-to-many relationship*/
public function hasManyTemplate(){ return $this->hasMany('App\Models\Demo\MakeTemplateFile','user_id','id'); } /* one-to-one relationship */ 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);
When using with, the associated fields should also be added. If not, there may be a null value (or null)