do you have $with in your models?
Apr 13, 2021
3
Level 1
Prevent to loaded One to One relationship on "Laravel API Resource"
Hello guys,
I have tried on docs laravel it's called "whenLoaded" on Laravel API Resource, the problem here is whenLoaded is work on Many relationship meanwhile one relationship must be called, so how can i prevent one to one is loaded on API Resource, considere this bellow code
Travel has one to Schedule
and Schedule one to Travel
and my resource like this
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class TravelResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'schedule' => new TravelScheduleResource($this->schedule),
];
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class TravelScheduleResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'travel' => new TravelResource($this->travel),
];
}
}
and my controller returned
return new ScheduleResource($resource);
and the result get an error
Maximum stack depth exceeded
Thanks in advance for reading my problem :)
Please or to participate in this conversation.