File: /mnt/data/ghayatcom/ghayatcom-api/app/Http/Resources/NotificationCollection.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class NotificationCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
private $code;
private $unread_count;
public function __construct($resource, $code = 200, $unread_count=0, $user_role = 'user')
{
parent::__construct($resource);
$this->code = $code;
$this->unread_count = $unread_count;
$this->user_role = $user_role;
}
public function toArray($request)
{
return [
'code' => $this->code,
'message' => [__('digimed_validation.success_response.notification_fetch_success')],
'data' => [
'unread_count' => $this->unread_count,
'notify' => $this->collection->map(function ($item) {
$data = $item->data;
if($this->user_role == 'admin') {
$data = json_decode($item->data);
}
/**
* @var object $item
*/
return [
'id' => $item->id,
'type' => $item->type,
'notifiable_type' => $item->notifiable_type,
'notifiable_id' => $item->notifiable_id,
'data' => $data,
'read_at' => $item->read_at,
'admin_read_at' => $item->admin_read_at,
'created_at' => $item->created_at,
'updated_at' => $item->updated_at,
'deleted_at' => $item->deleted_at,
];
}),
]
];
}
public function withResponse($request, $response)
{
/** @var array $jsonResponse */
$jsonResponse = json_decode($response->getContent(), true);
unset($jsonResponse['links']);
$response->setContent(json_encode($jsonResponse));
}
}