@princeoo7 if you want to transform the output of a query before presenting it to the view, then you should look at API Resources
https://laravel.com/docs/8.x/eloquent-resources#introduction
This will allow you to prepare the output for your json.
eg:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class User extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
see https://laravel.com/docs/8.x/eloquent-resources#concept-overview
so in your case, your toArray method may look like:
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'attributes' => $this->attributes
];
}