Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel pass parameter to resource collection

class PatientResource extends JsonResource
{

  private static $data;

  public function toArray($request)
  {
    //access $data
    //self::$data
    return [
      'id' => $this->id,
      'first_name' => $this->first_name,
      'middle_name' => $this->middle_name,
      'last_name' => $this->last_name,
      'contact_number' => $this->contact_number
    ];
  }

  // Returns collection type
  public static function customCollection($resource, $data): IlluminateHttpResourcesJsonAnonymousResourceCollection
  {
   // You can add as many params as you want.
    self::$data = $data;
    return parent::collection($resource);
  }
}

// In controller I called that custom function.

$data = PatientResource::customCollection($query->get(),$medicines);
 
PREVIOUS NEXT
Tagged: #laravel #pass #parameter #resource #collection
ADD COMMENT
Topic
Name
5+1 =