birim/laravel-rest-api
Expose Eloquent models as a simple JSON REST API in Laravel. Configure endpoints in a config file, then query /laravel-json/{resource} for lists, skip/take pagination, and basic field search. Optionally control returned attributes via model properties.
The Laravel REST API provides an interface for applications to interact with your application by sending and receiving data as JSON.
You can install the package via composer:
composer require birim/laravel-rest-api
You can publish the config file with:
php artisan vendor:publish --tag=laravel-rest-api
First define the endpoints for your REST API in the configuration file. An endpoint contains a label and refers to an Eloquent class. Example:
<?php
return [
'endpoints' => [
'users' => App\Models\User::class
]
];
After configuration, the REST API endpoints can be called. All endpoints start with laravel-json.
Currently, three types of endpoints are provided: List, Pagination and Search.
| URI | Method | Description |
|---|---|---|
| /laravel-json/users | GET | Get all users |
| /laravel-json/users/skip/0/take/10 | GET | Get users by skip and take |
| /laravel-json/users/search/name/john | GET | Get all users with the name john |
All available endpoints can be accessed with /laravel-json
Returned data can optionally be configured within the Eloquent Model files. The $restApiAttributes property defines all attributes that should be returned. Without this property all attributes will be returned. If you want to exclude only certain attributes, you can use the $restApiHidden property.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ExampleModel extends Model
{
public $restApiAttributes = [
'name',
'email'
];
public $restApiHiddenAttributes = [
'name'
];
Please read CHANGELOG for more information of what was changed recently.
This project and the Laravel framework are open-sourced software licensed under the MIT license.
How can I help you explore Laravel packages today?