Php Laravel空间查询生成器-进行连接时查询错误
我对软件包Spatiale/Laravel查询生成器有问题。我使用这个包是为了方便地过滤和排序我的查询,但它不是这样的:D 我试图过滤有两个关系的结果-商店和员工。简言之,它希望筛选存储报告列表 看,这是我的密码。当我使用连接方法时,作为响应,我会收到ID不正确的数据。当注释连接方法时,一切正常,但我需要按关系排序Php Laravel空间查询生成器-进行连接时查询错误,php,laravel,eloquent,relation,laravel-query-builder,Php,Laravel,Eloquent,Relation,Laravel Query Builder,我对软件包Spatiale/Laravel查询生成器有问题。我使用这个包是为了方便地过滤和排序我的查询,但它不是这样的:D 我试图过滤有两个关系的结果-商店和员工。简言之,它希望筛选存储报告列表 看,这是我的密码。当我使用连接方法时,作为响应,我会收到ID不正确的数据。当注释连接方法时,一切正常,但我需要按关系排序 return ReportControlResource::collection( QueryBuilder::for(Report::class)
return ReportControlResource::collection(
QueryBuilder::for(Report::class)
->with(['employee', 'shop'])
->allowedFilters('shop.name', 'employee.name')
->join('employees', 'employees.id', '=', 'reports.employee_id')
->join('shops', 'shops.id', '=', 'reports.shop_id')
->allowedSorts(['employees.name'])
->get()
);
我在报告模型中的关系:
public function shop(): BelongsTo
{
return $this->belongsTo(Shop::class);
}
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class);
}
商店模型中的关系:
public function reports()
{
return $this->hasMany(Report::class);
}
在员工模型中
public function reports()
{
return $this->hasMany(Report::class);
}
你有什么想法吗
我注意到ID被覆盖了,但不是店铺ID和员工ID,而是ID为什么被覆盖
我认为问题在于图书馆本身。创建者没有像我的例子那样考虑连接表的相反情况
请看文档中的示例:
$addRelationConstraint = false;
QueryBuilder::for(User::class)
->join('posts', 'posts.user_id', 'users.id')
->allowedFilters(AllowedFilter::exact('posts.title', null, $addRelationConstraint));
我的加入
->join('employees', 'employees.id', '=', 'reports.employee_id')
但是这项工作是这样的
->join('employees', 'reports.id', '=', 'reports.employee_id')
但是为什么呢?我用多种方法检查了顺序,甚至断开了过滤,它没有改变任何东西我找到了问题的解决方案。我必须选择一个专栏,现在所有的作品都很好:-) 这不是干净的代码,但是当使用实现Sort创建自己的类时,代码看起来可能会更好 谢谢盖伊的帮助!祝你好运!:-)
return EvidenceControlResource::collection(
QueryBuilder::for(EvidenceControl::class)
->allowedIncludes('employee', 'shop')
->select('evidence_controls.*', DB::raw('employees.id as employee_id'))
->join('employees', 'evidence_controls.employee_id', '=', 'employees.id')
->select('evidence_controls.*', DB::raw('shops.id as shop_id'))
->join('shops', 'evidence_controls.shop_id', '=', 'shops.id')
->allowedFilters('shop.name', 'employee.name')
->allowedSorts(['employees.name', 'shops.name'])
->get()
);