install Package composer require ibnudirsan/lara-handler-sanctum
Ganti baris kode program ini :
// bootstrap/app.php
<?php
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class,
);
Menjadi seperti ini :
// bootstrap/app.php
<?php
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
Ibnudirsan\LaraHandlerSanctum\Exceptions\HandlerSanctumException::class,
App\Exceptions\Handler::class,
);
/**
* Method yang dapat digunakan
*/
return ResponseJson::cretae($result);
return ResponseJson::read($result);
return ResponseJson::show($result);
return ResponseJson::update();
return ResponseJson::delete();
// App/Http/Controllers/usersController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Ibnudirsan\LaraHandlerSanctum\Halper\ResponseJson;
class usersController extends Controller
{
public function getUser($id)
{
$result = User::where('id',$id)->first();
return ResponseJson::read($result);
}
}
// Contoh Return Json
{
"app": {
"info": {
"error": false,
"Status": "Read Data",
"httpcode": 200,
"Message": "Successfully Read Data"
},
"result": {
"data": {
"name": "ibnudirsan",
"email": "[email protected]"
}
}
}
}
Publish package configuration php php artisan vendor:publish --tag=handler-sanctum-config
Secara otomatis akan membuat file handler.php
// config/handler.php
<?php
return [
'hidden' => [
'email_verified_at',
'created_at',
'updated_at',
]
];
Note :
- Di file ini bisa menambahkan atau menguragi filed yang di hidden.