PHP code example of jornatf / laravel-api-json-response
1. Go to this page and download the library: Download jornatf/laravel-api-json-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/ */
jornatf / laravel-api-json-response example snippets
namespace App\Http\Controllers;
use App\Models\Post;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'de 200 for "Ok" or 201 for "Created"
return ApiResponse::response(201)
->addDatas($post)
->json();
}
}
namespace App\Http\Controllers;
use App\Models\Post;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
public function show(int $post_id)
{
return ApiResponse::find(Post::class, $post_id)->json();
}
}
// First, you can instantiate response with a status code (y find a model by id:
$response = ApiResponse::find(Model::class, int $id);
// To add custom message:
$response->addMessage(string $message);
// To add datas to return when success:
$response->addDatas(array $datas);
// To add details (e.g. validator errors) when error:
$response->addDetails(mixed $details);
// or
$response->addDetails(array $details);
$response->addDetails(array $details);
// Last, formate response into json (
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.