Search
 
SCRIPT & CODE EXAMPLE
 

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);
Comment

PREVIOUS NEXT
Code Example
Php :: get country from ip php 
Php :: php check name 
Php :: last day of previous month in php 
Php :: teruglopende for loop php 
Php :: php json hjeader 
Php :: wordpress disable posts 
Php :: wordpress get site url 
Php :: loop through months and year php 
Php :: erd in phpmyadmin 
Php :: laravel composer 
Php :: rus text check php 
Php :: /laravel-2020-07-27.log" could not be opened 
Php :: guzzlehttp post json example 
Php :: current page link using php 
Php :: email php using html 
Php :: how to delete a file in laravel 
Php :: php check if string or number 
Php :: Git delete single branch 
Php :: DB::rollback() 
Php :: get image size php 
Php :: how to fetch jQuery in wordpress 
Php :: php check if get var is set 
Php :: php executable not found visual studio code ubuntu 
Php :: clear laravel cache 
Php :: how to use required_with in laravel to array element 
Php :: laravel mongodb field delete (key delete) (column delete) drop 
Php :: swich in php 
Php :: array_map class method 
Php :: make controller laravel 8 with resource 
Php :: laravel get last month records 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =