crystoline/laravel-rest-api

Laravel Rest Api Library. Write produce rest apis very fast

v2.0.0 2022-02-15 14:35 UTC

This package is auto-updated.

Last update: 2022-11-18 10:36:03 UTC


README

68747470733a2f2f6c61726176656c2e636f6d2f6173736574732f696d672f636f6d706f6e656e74732f6c6f676f2d6c61726176656c2e737667

laravel-rest-api

Build Status Code Intelligence Status Code Quality Latest Stable Version Total Downloads License

Description

Provide rest apis in Laravel in easy steps. Avoid Repeated crud/Bread Process. This helps you to focus on the business logic

Say Thanks!

Installation

composer require crystoline/laravel-rest-api

##Usage

Controller Sample

<?php

namespace App\Http\Controllers;

use Crystoline\LaraRestApi\RestApiTrait;
use Crystoline\LaraRestApi\IRestApiAble;
use Crystoline\LaraRestApi\TestModel;

class TestController extends Controller implements IRestApiAble
{
    use RestApiTrait;
    
    /**
    * Define the Eloquent Model.
    * This line is required.   
    */
    public static function getModel() : string  {
        return TestModel::class;
    }
    
    
    public static function getValidationRules(): array
    {
        return [
            "name" => "required",
            "email" => "required|email",
            "password" => "required|password_confirmed"
        ];
    }


}

Route Sample

<?php

    Route::prefix('users')->group( function () {
        Route::get('', 'TestController@index');
        Route::get('show/{id}', 'TestController@show');
        Route::post('store', 'TestController@store');
        Route::put('update/{id}', 'TestController@update');
        Route::put('delete/{id}', 'TestController@destroy');

    });

or

<?php
    Route::resource('users', 'TestController');

Filter

You can add filter for the list method

<?php
namespace App\Http\Controllers;

use Crystoline\LaraRestApi\RestApiTrait;
use Crystoline\LaraRestApi\IRestApiAble;
use Crystoline\LaraRestApi\TestModel;
use Illuminate\Http\Request;
class TestController2 extends Controller implements IRestApiAble
{
    use RestApiTrait;
    
    /**
    * Define the Eloquent Model.
    * This line is required.   
    */
    public static function getModel() : string  {
        return TestModel::class;
    }
    public static function filter(Request $request, $query)
    {
        if($request->input('user_id')){
            $user_id = $request->input('user_id');
            $query->where(user_id, $user_id);
        }
    }
}

Search

Search has been provided by default, pass search query in query/post parameter ('search'')

<?php
namespace App\Http\Controllers;

use Crystoline\LaraRestApi\RestApiTrait;
use Crystoline\LaraRestApi\IRestApiAble;
use Crystoline\LaraRestApi\TestModel;
use Illuminate\Http\Request;
class TestController3 extends Controller implements IRestApiAble
{
    use RestApiTrait;
    
    /**
    * Define the Eloquent Model.
    * This line is required.   
    */
    public static function getModel() : string  {
        return TestModel::class;
    }
    public static function searchable(): array
   {
       return [
           'last_name', 'first_name'
       ];
   }
    
}

Sorting

<?php
namespace App\Http\Controllers;

use Crystoline\LaraRestApi\RestApiTrait;
use Crystoline\LaraRestApi\IRestApiAble;
use Crystoline\LaraRestApi\TestModel;
use Illuminate\Http\Request;
class TestController4 extends Controller implements IRestApiAble
{
    use RestApiTrait;
    
    /**
    * Define the Eloquent Model.
    * This line is required.   
    */
    public static function getModel() : string  {
        return TestModel::class;
    }
    public static function orderBy(): array
   {
       return [
           'last_name' => 'ASC'
       ];
   }
    
}

Events

You add events before and after controller action. beforeList,afterList, beforeStore, afterStore, beforeShow, afterShow etc.

<?php

namespace App\Http\Controllers;

use Crystoline\LaraRestApi\RestApiTrait;
use Crystoline\LaraRestApi\IRestApiAble;
use Crystoline\LaraRestApi\TestModel;
use Illuminate\Http\Request;

class TestController5 extends Controller implements IRestApiAble
{
    use RestApiTrait;
    
    /**
    * Define the Eloquent Model.
    * This line is required.   
    */
    public static function getModel() : string  {
        return TestModel::class;
    }
    
    
 public function beforeStore(Request $request): bool
    {
        $password = $request->input('password');
        if(!empty($password)) {
            $request->merge(['password' => bcrypt($password)]);
            return true;
        }
        return false;
    }
}