Home > database >  I have a problem on API Resources Laravel (Maximum stack depth exceeded)
I have a problem on API Resources Laravel (Maximum stack depth exceeded)

Time:08-11

i have a problem when i use API Resources inside another API Resources class like this:

if (! Route::is('job.*')) {
            $data['sites']= SiteResource::collection($this->sites);
           $data['jobs'] =  JobResource::collection($this->jobs);
 }

but when I remove the class the problem disappears like this :

 if (! Route::is('job.*')) {
                $data['sites']= $this->sites;
               $data['jobs'] =  $this->jobs;
     }

this is -> image for error

1

this is my code :

class CustomerResource extends JsonResource
{
    public function toArray($request)
    {

        $data = [
            'id' => $this->id,
            'name' => $this->name,
            'billing_details' => $this->billing_details,
            'billing_info' => [
                'address' => $this->billing->address,
                'street_num' =>$this->billing->street_num,
                'country' =>$this->billing->country->name,
                'city' =>$this->billing->city,
                'postal_code' =>$this->billing->postal_code,
                'credit_limit' =>$this->billing->credit_limit,
                'payment_term_id' =>$this->billing->payment_term_id,
                'send_statement'  =>$this->billing->send_statement
            ],
           'contacts' => $this->contacts,
            'sitecontact' => $this->sitecontact,

        ];


        if (! Route::is('job.*')) {
            $data['sites']= SiteResource::collection($this->sites);
           $data['jobs'] =  JobResource::collection($this->jobs);
        }
        return $data;
    }
}

CodePudding user response:

I called CustomerRessource class on JobRessource class which leads to an infinite loop between them

JobRessource class

if (! Route::is('job.*')) {
      $data['sites']= SiteResource::collection($this->sites);
      $data['jobs'] =  JobResource::collection($this->jobs);
   }

I fixed it by using this condition on JobRessource

  if (Route::is('job.*')) {
       $data['customer' ] = new CustomerResource($this->customer);
   }

JobRessource with condition

@N69S thank you for your comment

  • Related