3

I am new to Laravel. I am currently building a resource. I was just wondering what is the purpose of passing in the $request object if we use the $this variable when using resources. Below is an example of my code.

class PetitionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)

    {
        return ["id"=> $this->id,"title"=>$this->title,'author'];
    }
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
json2021
  • 2,146
  • 2
  • 14
  • 28

0 Answers0