I want (due to the project) to create an array in the class controller and pass it to the resource. In my controller class, consider this method:
public function getExample(){
$attribute=array('otherInfo'=>'info');
return new ExampleResource($attribute);
}
and in my class I would write something like ExampleResource with:
public function toArray($request){
return[
'info' => $this->info
];
}
How can I convert the value of $ attribute to perform this operation return new ExampleResource($attribute);
?
Please do not suggest me to insert information about the field in the model, this attribute can come only from the external, from the controller and does not apply to the model in the database.
class ExampleResource extends Resource
{
private $info;
public function __construct($info)
{
$this->$info = $info;
}
public function toArray($request)
{
return[
'info'=>$this->$info,
'id' => $this->id
];
}
}
source
share