Nov 4, 2019
0
Level 3
send multiple data and show them in resource api laravel
i have 2 variables that i want to send them to resource and show them in api like below :
return new RoomDetailResource($data_array,$sum);
and the resource i have written is like below :
class RoomDetailResource extends JsonResource
{
/**
* @var
*/
public $sum;
/**
* Create a new resource instance.
*
* @param mixed $resource
* @return void
*/
public function __construct($resource, $sum)
{
// Ensure you call the parent constructor
parent::__construct($resource);
$this->resource = $resource;
$this->sum = $sum;
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'sum' => $this->sum
];
}
}
now what i get is the error :
"message": "Trying to get property 'id' of non-object",
"status_code": 500,
but if i want to show sum its shows without any problem now here is how i want my api :
{
id:1,
sum:200
},
{
id:2,
sum:200
}
note that sum is the same for all and i just want to repeat it in objects or show them in the end of api as a property . thanks
Please or to participate in this conversation.