CGS4k

Laravel Api Controller

Introduction

This package automates many of your CRUD actions for API's in Laravel.
Its as quick as running php artisan make:api {{MODEL}} which dependant on your options will create the following:

  • Eloquent Model
  • Api Controller
  • Policy File
  • Request File
  • Response Resource File
  • API Route entry

you will now have an working api that you can customise as needed.

Features

  • API CRUD Actions by default with dynamic filtering on the list method, : eg: /api/your_model?filter[field]=x&filter[another!]=y&limit=5
  • Scope aware and can include scopes on the filters: eg: /api/your_model?myScope=x
  • Uses Laravel Gates so most permission addon packages work (tested with pure Laravel Gates & Spatie permissions package)
  • Events on API
  • Can deal with relationships - hasOne, hasMany, BelongsToOne, BelongsToMany for retrieving and saving
  • Automatic mapping from camel to snake / snake to camel based on your settings -- no more having to use mappers in your frontend code / backend code
  • Pagination
  • Validation
  • Response field protection / mapping
  • artisan command to generate full api per eloquent model
  • actively maintained and developed as in production environment on all our Laravel Projects

Usage

Generate a new Api Controller, Repository and Route via php artisan make:api {ModelName}

This will create an Api/ModelNameController for you and you will have the basic routes in place as follows:

  • GET api/v1/{model_name} - list all/paged/filtered (class::index)
  • GET api/v1/{model_name}/$id - Show a specified id (class::show)
  • POST api/v1/{model_name} - Insert a new record (class::store)
  • PUT api/v1/{model_name}/$id - Update an existing record (class::update)
  • DELETE api/v1/{model_name}/$id - Delete an existing record (class::destroy)