1. Go to this page and download the library: Download mesak/laravel-api-response library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mesak / laravel-api-response example snippets
namespace App\Http\Controllers;
use Mesak\LaravelApiResponse\Http\Controllers\ApiController as BaseController;
class MainController extends BaseController
{
}
class MainController extends BaseController
{
function index(Request $request)
{
return \App\Models\User::paginate(15);
}
}
class MainController extends BaseController
{
function index(Request $request)
{
$users = \App\Models\User::paginate(15);
return \App\Http\Resources\User::collection($users);
}
}
class MainController extends BaseController
{
function index(Request $request)
{
$users = \App\Models\User::paginate(15);
if( $users->isEmpty() )
{
throw new \Exception('No users found');
}
return \App\Http\Resources\User::collection($users);
}
}
class MainController extends BaseController
{
function index(Request $request)
{
$users = \App\Models\User::paginate(15);
if( $users->isEmpty() )
{
throw new \Mesak\LaravelApiResponse\Exceptions\BaseException('No users found' ); //statusCode 500
}
return \App\Http\Resources\User::collection($users);
}
}
namespace App\Exceptions;
class BadRequestException extends \Mesak\LaravelApiResponse\Exceptions\BaseException
{
protected $errorCode = 400;
protected $statusCode = 400;
public function __construct($message = 'Bad Request')
{
parent::__construct($message);
}
}
use App\Exceptions\BadRequestException;
class MainController extends BaseController
{
function index(Request $request)
{
$users = \App\Models\User::paginate(15);
if( $users->isEmpty() )
{
throw new BadRequestException(); //statusCode 400
}
return \App\Http\Resources\User::collection($users);
}
}