php - Laravel Eloquent query/subquery with no values throws error
问题描述
All of my departments need to submit various reports each quarter: budget, HR, etc. I want to show a table that shows all of the required reports for the logged-in user's department (all departments must submit the same reports), a boolean if they have submitted it, and a boolean if that file is considered valid. I'm having problems mostly with the boolean values.
I have three tables: reports
stores the id
, name
, and due_date
; file_uploads
stores the id
, report_id
, filename
, and department_id
; file_upload_errors
stores id
, file_upload_id
, and error
.
In order to build the table to display to the user, I've queried the Reports table:
$data = Reports::with([
'file_uploads' => function ($query) {
$query->where('department_id', '=', user()->department_id);
},
'file_uploads.file_upload_errors'
])->get();
Next, I need to map the data to my view model:
$vm = array();
//map the data to the view model
foreach ($data as $value) {
$mapping = new DataRequestsViewModel();
$mapping->ReportName = $value->name;
$mapping->DueDate = $value->due_date;
$mapping->IsUploaded = ($value->file_uploads->count() > 0) ? true : false;
$mapping->IsValid = ($value->file_uploads->file_upload_errors->count() < 1) ? true : false;
$vm[] = $mapping;
}
However, I'm getting an exception: Property [file_upload_errors] does not exist on this collection instance.
I understand this is because if the user has not yet uploaded the file, then it can't query the relationship of null
to see if there are any file_upload_errors
but I don't know how to handle it... Can someone show me a good approach?
I didn't actually end up using empty()
when all is said and done, but it did lead me down the right path... here's what I ended up with:
foreach ($data as $value) {
$mapping = new DataRequestsViewModel();
$mapping->TemplateId = $value->id;
$mapping->TemplateName = $value->name;
$mapping->DueDate = $value->due_date;
$mapping->IsUploaded = ($value->file_uploads->count() > 0) ? true : false;
if ($mapping->IsUploaded == false) {
$mapping->IsValid = false;
} else {
$mapping->IsValid = $value->file_uploads->first()->file_upload_errors->count() < 1 ? true : false;
}
$vm[] = $mapping;
}
解决方案
我完全在这里猜测,但我认为这是因为您正在对您的 进行自定义查询file_uploads
,您实际上会丢失内置file_uploads.file_upload_errors
映射。
移动查询with
内部file_uploads
,它将正确地将其附加到该集合中。
也许这会起作用:
$data = Reports::with([
'file_uploads' => function ($query) {
$query->where('department_id', '=', user()->department_id)
->with('file_upload_errors');
},
])->get();
推荐阅读
- go - go vet 抱怨循环变量被 func 字面量捕获
- linux - 如何在 Bash 脚本中将每个元素从 for 循环设置为 var?
- html - 如何删除页面中特定宽度的内容?
- linux - 是否有一个内置的 linux 命令返回代码匹配一些整数输入参数?
- sql - 如何在 SQL 数据库上使用 dplyr `distinct()` 函数?
- ms-access - 表单中表字段的 ColumnHistory
- sql - SQL:差距和孤岛问题 - 日期不连续导致排名不准确
- aws-lambda - AWS Lambda 中数据库代理的目的是什么?
- sql - 如何在 BigQuery 中解析 json 列表
- sql-server - 使用不同的 where 子句向我的查询添加另一列