1、在laravel使用with关联模型的时候,需要指定字段输出,这个时候,发现返回的值为空,经排查,是因为关联的id没有全部放出来查询:
public function user(){ return $this->hasMany('App\Model\UserModel', 'org_id', 'id'); }
//返回空
Org::with(array('user'=>function($query){ $query->select('username'); }))->get();
需把关联的字段也查询,才有数据:
Org::with(array('user'=>function($query){ $query->select('id','org_id','username'); }))->get();
转载请注明:永盟博客 » laravel使用模型关联with问题