Answer the question
In order to leave comments, you need to log in
How to perform dependency injection in resource?
I am using resource to format the API response. Some
objects have links to files, and for these links you need to create a signature and a lifetime.
This action is performed by a class object, and I want to inject it like this:
class AttachmentResource extends JsonResource
{
/**
* @var S3Client
*/
protected $s3Client;
public function __construct($resource, S3Client $client)
{
parent::__construct($resource);
$this->s3Client = $client;
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$cmd = $this->s3Client->getCommand('GetObject', [
'Bucket' => config('aws.bucket'),
'Key' => $this->url
]);
$preSignedRequest = $this->s3Client->createPresignedRequest($cmd,'+1 year');
return [
'id' => $this->id,
'url' => $preSignedRequest->getUri()
];
}
}
Argument 2 passed to App\\Http\\Resources\\AttachmentResource::__construct() must be an instance of Aws\\S3\\S3Client, integer given, called in /var/www/vendor/laravel/framework/src/ Illuminate/Support/Collection.php on line 1039"
AttachmentResource::collection($this->attachments);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question