【发布时间】:2019-07-26 05:46:17
【问题描述】:
我有一个查询,它返回以下我传递给资源集合的 json。问题是它正确返回了所有字段,但是当涉及到 Companies_close 字段时,它显示为空。而 company_close 是一个数组或空数组。我不明白为什么会这样。
我在 Laravel 5.6.39 中使用了同样的方法(通过一堆函数传递数据)它在 Laravel 中工作得很好。但是当我在 lumen 5.6.4 中执行此操作时,它不起作用。
我认为这可能是缓存问题,所以我几乎遵循了这个 url 中的所有方法,但没有成功。由于它不起作用,我恢复了所有更改。 https://github.com/laravel/framework/issues/2501
我找不到更多关于 lumen api 资源收集的资料。任何人都可以看看并告诉我我做错了什么。任何帮助将不胜感激。
雄辩的查询:
$companiesQuery = CpdAdmin::where('admin_id', $admin->admin_id)
->with(['companies' => function($query) use($request) {
$query->where('air_id', $request->airport_id)
->where('status', '1')
->with(['companiesClosed' => function($query) use($request) {
$query->where('close_status', 'active')
->where(function ($queryWhere) use($request) {
$queryWhere->where('start_date', '>=', $request->start_date)
->where('start_date', '<=', $request->end_date)
->where(function ($queryType) {
$queryType->where('journey_type', 'Departure')
->orWhere('journey_type', 'Both');
});
})
->orWhere(function ($queryWhere) use($request) {
$queryWhere->where('end_date', '>=', $request->start_date)
->where('end_date', '<=', $request->end_date)
->where(function($queryType) {
$queryType->where('journey_type', 'Arrival')
->orWhere('journey_type', 'Both');
});
});
}]);
}])
->get();
JSON(在将其传递给资源集合之前):
将它传递给集合之前的 json 返回工作正常,我以我期望的方式显示一切。
"data": [
{
"admin_id": 32,
"admin_name": "Eden Parking",
"email": "edenparking@hotmail.com",
"admin_login": "edenparking@hotmail.com",
"admin_information": "what information",
"contact_number": 303633,
"address": "paka dubai da",
"invoice_email": "invoice@gmail.com",
"contact_person": "uzair khan",
"admin_rights": "1114",
"admin_isactive": "1",
"edit_status": 0,
"created_at": null,
"updated_at": null,
"companies": [
{
"comp_id": 19,
"comp_title": "S Gatwick Meet & Greet",
"comp_email": "mcpgatwick@gmail.com",
"status": 1,
"operation_data": "{\"operating_type\":\"24_7\",\"opening_status\":\"open\"}",
"daily_bookings": 100,
"monthly_bookings": 1000,
"comp_admin": 32,
"air_id": 14,
"companies_closed": []
},
{
"comp_id": 57,
"comp_title": "Simply Parking Gatwick Meet & Greet",
"comp_email": "mcpgatwick@gmail.com",
"status": 1,
"operation_data": "{\"operating_type\":\"24_7\",\"opening_status\":\"open\"}",
"daily_bookings": 100,
"monthly_bookings": 1000,
"comp_admin": 32,
"air_id": 14,
"companies_closed": [
{
"id": 3101,
"start_date": "2019-03-04",
"end_date": "2019-03-15",
"journey_type": "Departure",
"close_status": "active",
"comp_id": 57,
"created_at": null,
"updated_at": null
},
{
"id": 3102,
"start_date": "2019-03-04",
"end_date": "2019-03-15",
"journey_type": "Both",
"close_status": "active",
"comp_id": 57,
"created_at": null,
"updated_at": null
}
]
},
{
"comp_id": 149,
"comp_title": "Park with us Gatwick",
"comp_email": "mcpgatwick@gmail.com",
"status": 1,
"operation_data": null,
"daily_bookings": 100,
"monthly_bookings": 1000,
"comp_admin": 32,
"air_id": 14,
"companies_closed": []
}
]
}
]
这里我将查询传递给资源集合
$companiesQueryResourceCollection = new CompanyResourceCollection($companiesQuery);
资源收集:
在我的最后一个函数中
'companies_closed' => $eachCompany->companies_closed
我可以循环 Companies_closed 但如果我能得到一些东西,如果我什么也没得到,foreach 只会抛出一个错误。
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CompanyResourceCollection extends ResourceCollection
{
public function toArray($request)
{
$response = $this->transformMultiple();
return $response;
}
public function transformMultiple()
{
$transform = [];
foreach ($this->collection as $item)
{
$transform[] = $this->transformSingle($item);
}
return $transform;
}
public function transformSingle($item)
{
return [
'admin_id' => $item->admin_id,
'admin_name' => $item->admin_name,
'email' => $item->email,
'contact_number' => $item->contact_number,
'contact_person' => $item->contact_person,
'companies' => $this->transformCompanies($item->companies),
];
}
public function transformCompanies($companies)
{
$transform = [];
foreach ($companies as $singleCompany)
{
if( ($singleCompany->dailyBookingsCount < $singleCompany->daily_bookings) && ($singleCompany->monthlyBookingsCount < $singleCompany->monthly_bookings) )
{
// initially i was passing each company to its single resource but it was showing the same behaviour, so i moved the code to this file.
// $transform[] = new CompanyResource($singleCompany);
$transform[] = $this->transformEachCompany($singleCompany);
}
}
return $transform;
}
public function transformEachCompany($eachCompany)
{
return [
'comp_id' => $eachCompany->comp_id,
'comp_title' => $eachCompany->comp_title,
'comp_email' => $eachCompany->comp_email,
'status' => $eachCompany->status,
'operation_data' => json_decode($eachCompany->operation_data),
'daily_bookings' => $eachCompany->daily_bookings,
'monthly_bookings' => $eachCompany->monthly_bookings,
'comp_admin' => $eachCompany->comp_admin,
'air_id' => $eachCompany->air_id,
'dailyBookingsCount' => $eachCompany->dailyBookingsCount,
'monthlyBookingsCount' => $eachCompany->monthlyBookingsCount,
// this returns 0 for all
// 'companies_closed' => count($eachCompany->companies_closed)
// this returns null for all
// 'companies_closed' => $eachCompany->companies_closed
// I can pass this array to another function but that would throw an error because the companies_closed is empty
// 'companies_closed' => $eachCompany->companies_closed
];
}
}
【问题讨论】: