Skip to content

Instantly share code, notes, and snippets.

@robyfirnandoyusuf
Created August 9, 2021 04:07
Show Gist options
  • Save robyfirnandoyusuf/b5ab556509cead4c314fc7fdd5c10526 to your computer and use it in GitHub Desktop.
Save robyfirnandoyusuf/b5ab556509cead4c314fc7fdd5c10526 to your computer and use it in GitHub Desktop.
Trait for API
<?php
/**
* @author Nando (c) 2018
* Simple API Trait
*/
namespace App\Traits;
trait APITrait
{
public $success = true;
public $data = null;
public $code = \Illuminate\Http\Response::HTTP_OK;
/**
* @return \Illuminate\Http\JsonResponse
*/
public function json() : \Illuminate\Http\JsonResponse
{
$result = array();
$result['success'] = $this->success;
$result['data'] = $this->data;
$result['code'] = $this->code;
return response()->json($result, $this->code, [], JSON_PRETTY_PRINT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment