skip to Main Content

I have successfully implemented the fetching of data, Am using loop to fetch the data. how do i pass it to collection;

my data now looks like when i try returning from the controller direct to the view:

0
    : 
    {id: 17043, clinic: 1, patient: 7932, unique_id: "63f37ee7a2cc3", purpose: null, external_doctor: null,…}
    1
    : 
    {id: 17002, clinic: 1, patient: 7935, unique_id: "63ce299c7383c", purpose: null, external_doctor: null,…}
     

In my controller I have this code which fetches the data from the InsuranceProforma entity and gets the visits_id then uses it to get data from Visits model of corresponding patient:

            $proformas = InsuranceProforma::all();
        $visits = [];
        
        foreach ($proformas as $proforma) {
            $visitId = $proforma->visit_id;
            $visit = Visit::findOrFail($visitId);
            $visits[] = $visit;
        }
    
        //$details = InsuranceProformaResource::collection($visits);
    //return $details;
        return $visits;

In my InsuranceProformaResource I have this code which converts the response into a Json responce to pass now to the view. But I’ve commented it out currently in controller:

    <?php

namespace IgniteFinanceTransformersinvoices;


use IlluminateHttpResourcesJsonResource;

class InsuranceProformaResource extends Resource
{
    public function toArray($request)
    {
        return [
            'proforma_no'=>$this->proforma_no,
            'visit_id' => $this->visit_id,
            'patient_name' => $this->visits->patient->full_name ?? '-',
            'patient_id' => $this->patient,
            'patient_no' => $this->patient_no ?? '-',
            'policy_number' => @$this->policy_number ?? '-',
            "scheme_name" => @$this->scheme_name ?? '-',
            "amount"=>$this->amount,
            'insurance_name' => $this->insurance_name ?? '-',
            'date'=>$this->created_at->format('Y-m-d'),
        ];
    }
}

How can I go about it to return data i.e how the patient name is passed?

currently am getting Call to a member function toBase() on array or when i try changing am getting Call to a member function first() on array

2

Answers


  1. Eager loading is a mechanism of fetching data from two or more laravel models using joins. This technique is used to solve N+1 query problem.

    Login or Signup to reply.
  2. In Laravel we can send response in JSON format to view using return json() function.
    Such as return Response::json(View(‘user.home’)->with(‘var’, $val)->render());
    Where $val may be an array or object. User is the folder name in view directory and home is the blade view from which we can easily get value using $var variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search