Orm Laravel延迟加载参考$this->;Load()自定义约束中的属性
我有一个模型上的关系,我需要在我的控制器上延迟加载它,但我需要使用原始模型中的属性执行查询约束。 所以我有点像:Orm Laravel延迟加载参考$this->;Load()自定义约束中的属性,orm,eloquent,laravel-5.2,constraints,Orm,Eloquent,Laravel 5.2,Constraints,我有一个模型上的关系,我需要在我的控制器上延迟加载它,但我需要使用原始模型中的属性执行查询约束。 所以我有点像: $Users = User::all(); $Users->load(['disputes' => function($query){ $query->where('property', $this->property ); }]); 但它返回未定义的属性$account\u number\u tu。 在执行load()时,如何确保$this引用$User
$Users = User::all();
$Users->load(['disputes' => function($query){
$query->where('property', $this->property );
}]);
但它返回未定义的属性$account\u number\u tu。
在执行load()时,如何确保$this引用$Users中的每个$User?
我尝试在模型中执行此约束,但是
$this->property;
返回null。在引擎盖下,load()
初始化用户
模型上的Builder
实例,然后将传递给load
的参数传递给Builder上的方法。有了它,您可以在load
方法中完成与With
方法相同的所有操作
要回答您的问题,您可以执行以下操作:
$Users = User::all();
$Users->load(['disputes' => function($query) {
return $query->select('disputes.*')
->leftJoin('disputes', 'disputes.user_id', '=', 'users.id')
->whereRaw('users.property = disputes.property');
}]);
这与:
$Users = User::with(['disputes' => function($query) {
return $query->select('disputes.*')
->leftJoin('disputes', 'disputes.user_id', '=', 'users.id')
->whereRaw('users.property = disputes.property');
}]);
在引擎盖下,load()
初始化User
模型上的Builder
实例,然后将传递给load
的参数传递给Builder上的方法。有了它,您可以在load
方法中完成与With
方法相同的所有操作
要回答您的问题,您可以执行以下操作:
$Users = User::all();
$Users->load(['disputes' => function($query) {
return $query->select('disputes.*')
->leftJoin('disputes', 'disputes.user_id', '=', 'users.id')
->whereRaw('users.property = disputes.property');
}]);
这与:
$Users = User::with(['disputes' => function($query) {
return $query->select('disputes.*')
->leftJoin('disputes', 'disputes.user_id', '=', 'users.id')
->whereRaw('users.property = disputes.property');
}]);
所以争议是另一个表,例如,如果用户有一个名为phone的属性,那么我可以在whereRaw('discovers.phone'=users.phone')?对的在用户
和争议
是表名的情况下(如果不是,则用前缀替换实际表名)。我实际尝试查询的表名是“争议”和“分数\争议\帐户”。这些都是MySQL中显示的名称,但我一直得到找不到的错误列。我像whereRaw一样进行查询('disferences.account\u id=score\u aware\u disferences\u accounts.id');它指定它找不到列“score\u aware\u confirations\u accounts.id”。它好像不尝试查看指定的表。用户
上的争议
关系定义为什么?ie:$this->hasMany(争议::类)代码>该关系的关键列名是什么?因此争议是另一个表,例如,如果用户有一个名为phone的属性,我可以在whereRaw('discovers.phone'=users.phone')?对的在用户
和争议
是表名的情况下(如果不是,则用前缀替换实际表名)。我实际尝试查询的表名是“争议”和“分数\争议\帐户”。这些都是MySQL中显示的名称,但我一直得到找不到的错误列。我像whereRaw一样进行查询('disferences.account\u id=score\u aware\u disferences\u accounts.id');它指定它找不到列“score\u aware\u confirations\u accounts.id”。它好像不尝试查看指定的表。用户
上的争议
关系定义为什么?ie:$this->hasMany(争议::类)代码>该关系的关键列名是什么?