Php 带关系的Laravel雄辩ORM滤波器
我使用的是laravel 5.8: 我需要从一个Php 带关系的Laravel雄辩ORM滤波器,php,laravel,eloquent,Php,Laravel,Eloquent,我使用的是laravel 5.8: 我需要从一个用户获取所有关系。 我已经通过以下方式与模型建立了关系: $id = auth()->user()->id; $obj = User::with('grupos.relatorios.cliente')->get()->find($id); 客户 public function relatorios(){ return $this->hasMany('App\models\Relatori
用户
获取所有关系。
我已经通过以下方式与模型建立了关系:
$id = auth()->user()->id;
$obj = User::with('grupos.relatorios.cliente')->get()->find($id);
客户
public function relatorios(){
return $this->hasMany('App\models\Relatorio');
}
用户:
public function cliente()
{
return $this->belongsTo('App\models\Cliente');
}
public function grupos(){
return $this->belongsToMany("App\models\Grupo", "acessos");
}
Grupo
public function color()
{
return $this->belongsTo('App\models\Color');
}
public function usuarios(){
return $this->belongsToMany('App\User', 'acessos');
}
public function relatorios(){
return $this->hasMany('App\models\Relatorio');
}
public function cliente()
{
return $this->belongsTo('App\models\Cliente');
}
public function grupo()
{
return $this->belongsTo('App\models\Grupo');
}
Relatorio
public function color()
{
return $this->belongsTo('App\models\Color');
}
public function usuarios(){
return $this->belongsToMany('App\User', 'acessos');
}
public function relatorios(){
return $this->hasMany('App\models\Relatorio');
}
public function cliente()
{
return $this->belongsTo('App\models\Cliente');
}
public function grupo()
{
return $this->belongsTo('App\models\Grupo');
}
我需要从一个用户
中按组
列出所有关系人
,并按客户
过滤。
到目前为止,我可以这样做:
$id = auth()->user()->id;
$obj = User::with('grupos.relatorios.cliente')->get()->find($id);
我的结果是:
{
"id": 2,
"name": "renan",
"email": "renan.daher@dstec.com.br",
"email_verified_at": null,
"cliente_id": 2,
"created_at": "2019-09-18 18:08:45",
"updated_at": "2019-09-18 18:08:45",
"grupos": [
{
"id": 2,
"descricao": "Tribut\u00e1rio",
"color_id": 1,
"created_at": "2019-09-13 18:14:58",
"updated_at": "2019-09-13 18:14:58",
"pivot": {
"user_id": 2,
"grupo_id": 2
},
"relatorios": [
{
"id": 1,
"descricao": "Relat\u00f3rio de CDA",
"link": "www.google.com.br",
"cliente_id": 1,
"grupo_id": 2,
"created_at": "2019-09-16 15:23:45",
"updated_at": "2019-09-16 15:23:45",
"cliente": {
"id": 1,
"descricao": "PMNI",
"created_at": null,
"updated_at": null
}
},
但是,如何使用报告->客户id
过滤用户->客户id
编辑Mathieu Ferre更改:
谢谢你的帮助,我做了一些你写的事情:
$grupos = Grupo::whereHas('relatorios', function($query){
$id = auth()->user()->id;
$query->whereHas('cliente', function($query) use ($id){
$query->where('user_id', $id);
});
})->with('relatorios.cliente')->get();
return $grupos->toJson();
我有一个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause' (SQL: select * from `grupos` where exists (select * from `relatorios` where `grupos`.`id` = `relatorios`.`grupo_id` and exists (select * from `clientes` where `relatorios`.`cliente_id` = `clientes`.`id` and `user_id` = 1)))
您应该从最终想要的模型开始,在本例中,Grupo
所以你必须这样做:
$grupos=Grupo::whereHas('relatorios',function($query){
$query->whereHas('cliente',函数($query)use($id){
$query->where('user\u id',auth()->user()->id);
});
})->with('relatorios.cliente')->get();
然后显示它:
foreach($grupos作为$grupo){
foreach($grupo->relatorios as$relatorio){
$relatorio->cliente
}
}
Hi XLannes,欢迎来到SO。我想,如果我理解了你的问题,那么这就是你想要的。我试过这样做,但它产生了sql错误。我对主要问题进行了编辑,修改了客户和用户之间的链接?基本上,您需要在链接到用户的对象上创建一个whereHas:)