Merhabalar, Laravel query-builder kullandığım bir projem var. Alanlar include edilebiliyor, sort, filter, limit vb her şey okey çok iyi bir şekilde çalışıyor. Ancak fields ile alanları çekerken bir sorun yaşıyorum. Sorun aslında query-builderi değil, Laravelin içerisinde modelde olmayan alanı resource ile döndüğümde toArray alanlarında olmayan alanlarda dizi olarak döndüğü için onlar null olarak geliyor. Bunu çözmeye çalışıyorum. Bilgisi olan var mı ? Kod örnekleri :
Bir api çağrısı için endpoint :
/partners?include[]=courses.contents&include[]=city&fields=id,company_name,city_id,courses.id,description,courses.company_id,courses.name,courses.contents.id,courses.contents.course_id,courses.contents.title,city.id,city.name
PartnerResource içindeki toArray methodu dönüşü:
return [
'id' => $this->id,
'company_name' => $this->company_name,
'email' => $this->email,
'phone_number' => $this->phone_number,
'description' => $this->description,
'fax' => $this->fax,
'city_id' => $this->city_id,
'district_id' => $this->district_id,
'city' => $this->whenLoaded('city', function () {
return new CityResource($this->city);
}),
'district' => $this->whenLoaded('district', function () {
return new DistrictResource($this->district)
;}),
'products' => $this->whenLoaded('products', function () {
return ProductResource::collection($this->products);
}),
'address_line' => $this->address_line,
'status' => $this->status,
'is_featured' => $this->is_featured,
'rank' => $this->rank,
'company_type_id' => $this->company_type_id,
'company_type' => $this->whenLoaded('companyType', function () {
return new CompanyTypeResource($this->companyType);
}),
'logo' => $this->logo ?: null,
'contact_person' => $this->contact_person ?? [],
'created_at' => $this->created_at?->toDateTimeString(),
'updated_at' => $this->updated_at?->toDateTimeString(),
];
Ve endpoint json çıktısı :
{
"id": 7,
"company_name": "Topçuoğlu Ltd. Şti.",
"email": null,
"phone_number": null,
"description": null,
"fax": null,
"city_id": 69,
"district_id": null,
"city": {
"id": 69,
"name": "Bayburt"
},
"address_line": null,
"status": null,
"is_featured": null,
"rank": null,
"company_type_id": null,
"courses": [
{
"id": 14,
"name": "Consequatur nisi quia.",
"description": null,
"prerequisites": null,
"status": null,
"category_id": null,
"price": null,
"total_duration": null,
"company_id": 7,
"image": null,
"contents": [
{
"id": 148,
"course_id": 14,
"title": "Impedit magni ut cum aspernatur velit explicabo minus.",
"content_type": null,
"content_url": null,
"duration": null,
"rank": null,
"status": null,
"created_at": null,
"updated_at": null
},
{
"id": 149,
"course_id": 14,
"title": "Quo praesentium consequatur ut rem.",
"content_type": null,
"content_url": null,
"duration": null,
"rank": null,
"status": null,
"created_at": null,
"updated_at": null
},
{
"id": 150,
"course_id": 14,
"title": "Necessitatibus eius deserunt facilis eos expedita.",
"content_type": null,
"content_url": null,
"duration": null,
"rank": null,
"status": null,
"created_at": null,
"updated_at": null
},
{
"id": 151,
"course_id": 14,
"title": "Illum corporis quaerat debitis quae et animi eligendi optio.",
"content_type": null,
"content_url": null,
"duration": null,
"rank": null,
"status": null,
"created_at": null,
"updated_at": null
},
{
"id": 152,
"course_id": 14,
"title": "Dicta voluptatem et debitis et voluptates.",
"content_type": null,
"content_url": null,
"duration": null,
"rank": null,
"status": null,
"created_at": null,
"updated_at": null
},
{
"id": 153,
"course_id": 14,
"title": "Quis animi deserunt quo.",
"content_type": null,
"content_url": null,
"duration": null,
"rank": null,
"status": null,
"created_at": null,
"updated_at": null
},
{
"id": 154,
"course_id": 14,
"title": "Recusandae quam quisquam ratione sit.",
"content_type": null,
"content_url": null,
"duration": null,
"rank": null,
"status": null,
"created_at": null,
"updated_at": null
},
{
"id": 155,
"course_id": 14,
"title": "Rerum labore omnis maxime quod facere.",
"content_type": null,
"content_url": null,
"duration": null,
"rank": null,
"status": null,
"created_at": null,
"updated_at": null
}
],
"learning_outcomes": null,
"rating": "4.00",
"created_at": null,
"updated_at": null
}
],
"logo": null,
"contact_person": [],
"created_at": null,
"updated_at": null
},
Amacım : fieldsde eklemediğim alanların jsonda dönmemesi.