Looking to hire Laravel developers? Try LaraJobs

laravelapiresponses maintained by athosbenther

Description
This package is intend to help create standardized JSON responses using Laravel | PHP applications.
Author
Last update
2024/11/04 22:09 (dev-feature/2-add-paginated-response)
License
Links
Downloads
320
Tags

Comments
comments powered by Disqus

Laravel(?) API Responses

This package is intend to help create standardized JSON responses using Laravel | PHP applications.

Example Response Structure

{
    "message": "User retrieved!",
    "data": {
        "Name": "Jhon Doe"
    },
    "status": {
        "code": 200,
        "message": "OK"
    },
    "meta": {
        "timestamp": "2024-09-21 14:36:24"
    }
}

Instalation

Requirements

  • PHP 8.3+

Install with composer:

composer require athosbenther/laravelapiresponses

Usage

Add the trait ApiResponses to your class and use it to generate default responses. Or create a new ApiResponse directly.

The return class ApiResponse inherits from Symfony\Component\HttpFoundation\Response, and can be further configured using it's functions.

Using the Trait

<?php

use AthosBenther\LaravelApiResponses\Traits\ApiResponses;

class UserController
{
   use ApiResponses;

   public function getUser(Request $request, $id)
   {
       $user = User::find($id);

       return $this->Response(
           'User retrieved',
           $user,
           200
       );
   }

   public function addUser(AddUserRequest $request)
   {
       $user = new User($request->all());



       return $this->Response(
           'User created',
           $user,
           201
       );
   }
}

Using the Class

<?php

use AthosBenther\LaravelApiResponses\ApiResponse;

class UserController
{
   use ApiResponses;

   public function getUser(Request $request, $id)
   {
       $user = User::find($id);

       return new ApiResponse(
           'User retrieved',
           $user,
           200
       );
   }

   public function addUser(AddUserRequest $request)
   {
       $user = new User($request->all());



       return new ApiResponse(
           'User created',
           $user,
           201
       );
   }
}