Giter VIP home page Giter VIP logo

lara-core's Introduction

Lara Core

Quickly and easily create CRUD for Laravel API using these handy base classes.

Installation

composer require ashimbadrie/lara-core

Pre-Requisites

This package expects your database migrations to include the following required fields. By default, we turned off laravel's timestamp usage and opt-in for our custom create/update UNIX timestamp. We also added the ability to log who created/updated a record once authenticated.

$table->integer('created_by')->nullable()->comment('User who created the record');
$table->integer('created_on')->nullable()->comment('The date of creation in unix timestamp');
$table->integer('modified_by')->nullable()->comment('User who modified the record');
$table->integer('modified_on')->nullable()->comment('The date of modification in unix timestamp');

Creating a Data Model

A data model is the entrypoint to creating records in a database table. When a record is saved, the model tracks which authenticated user creates/modifies a record and also tracks the UNIX timestamp for these operations.

Creating a data model is as simple as follows:

class ExampleModel extends BaseModel {

  protected $table = 'example_table';
  
  protected function defaultLookupField(): String
  {
      return '';
  }
  
}

REST API CRUD

To handle basic REST API CRUD operations, we need to create a data manager and data controller as follows:

class ExampleManager extends DataManager {

  public function __construct()
  {
      $model = "App\Models\ExampleModel"; // Path to the ExampleModel we created above
      parent::__construct($model);
  }
  
}

class ExampleController extends DataController {

  private $exampleManager;

  public function __construct()
  {
      $this->exampleManager = new ExampleManager();
      parent::__construct($this->exampleManager);
  }
  
}

Once we have our data model, data controller and data manager set up we can now create our API resources to point to the CRUD logic. Inside the routes/api.php file, we add the following:

Route::get('examples/{id}', 'ExampleController@show');
Route::post('examples', 'ExampleController@store');
Route::patch('examples/{id}', 'ExampleController@update');
Route::delete('examples/{id}', 'ExampleController@destroy');

Paginated Record Listing

In order to load a paginated list of records, we need to create a list manager and list controller as follows:

class ExampleListManager extends DataListManager
{
    public function __construct()
    {
        $model = "App\Models\Example";
        parent::__construct($model);
    }
}

class ExampleListController extends DataListController
{
    private $exampleListManager; 
    
    public function __construct()
    {
        $this->exampleListManager = new ExampleListManager();
        parent::__construct($this->exampleListManager);
    }
}

Once we have our data list controller and data list manager we can now create our API resources to point to the listing logic. Inside the routes/api.php file, we add the following:

Route::post('examples/list', 'ExampleListController@page');
Route::get('examples/list', 'ExampleListController@index');

The listing post request accepts an application/json payload to load the first page as follows:

{
  "start": 0,
  "limit": 10,
  "sort_by": {},
  "filter_by": {}
}

NOTE: Your frontend will need to increment the start value in the payload above to cycle through a paginated list.

A typical response looks as follows:

{
  "page": [],
  "total": 0
}

Add filters to listing

TODO

Sort listing

TODO

lara-core's People

Contributors

ashimbadrie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.