gogilo/laravel-api-response-helpers

Laravel Api Response Helpers

v1.0 2021-10-25 16:15 UTC

This package is auto-updated.

Last update: 2025-03-29 00:52:51 UTC


README

Laravel Api Response Helpers for consistent response in laravel REST apis

Installation

Add repositories to composer.json

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/gogilo2003/laravel-api-response-helpers"
    }
]
    

Run composer require

composer require gogilo/laravel-api-response-helpers

Use this trait in with your api controller

Example

class UserController extends Controller
{
    use Ogilo\ApiResponseHelper;

    public function store(Request $request){
        $validator = Validator::make($request->all(),[
            'name'=>'required',
            'email'=>'required|email|unique:users,email',
            'password'=>'required|min:6',
        ]);

        if($validator->fails()){
            return $this->validationError($validator);
        }

        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();

        return $this->storeSuccess('User Created',['user'=>$user]);
    }
}

Methods

$this->validationError($validator)
   
$this->storeSuccess($message, $data = [])

$this->updateSuccess($message, $data = [])

$this->deleteSuccess()

$this->importSuccess($message, $data = [])

Feel free to leave comments for improvement