sanchescom / laravel-rest
This library provides tools and interfaces for working with REST API with Models and Collections.
Installs: 1 353
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 2
Open Issues: 2
Requires
- php: ^7.2
- guzzlehttp/guzzle: ^6.3
- jenssegers/model: ^1.2
- laravel/framework: 5.8.*
- sanchescom/json-helper: ^0.1
Requires (Dev)
- donatj/mock-webserver: ^2.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-10-28 20:20:41 UTC
README
This library provides tools and interfaces for working with REST API and using Laravel Models and Collections.
Installing
Require this package, with Composer, in the root directory of your project.
$ composer require sanchescom/laravel-rest
Laravel:
After updating composer, add the ServiceProvider to the providers array in config/app.php
'providers' => [ ... Sanchescom\Rest\RestServiceProvider::class, ],
Lumen:
After updating composer add the following lines to register provider in bootstrap/app.php
$app->register(Sanchescom\Rest\RestServiceProvider::class);
Configuration
Change your default rest api name in config/rest.php
:
'default' => env('REST_CLIENT', 'localhost'),
And add a new api configuration:
<?php return [ 'clients' => [ 'localhost' => [ 'provider' => 'guzzle', 'base_uri' => 'https://localhost/', 'options' => [ 'headers' => [ 'Content-Type' => 'application/json', ], ], ], ], ];
Model
This package includes a Rest enabled Model class that you can use to define models for corresponding collections.
<?php use Sanchescom\Rest\Model; class User extends Model { /** {@internal} */ protected $dataKey = 'data'; /** {@internal} */ protected $fillable = [ "id", "first_name", "last_name", "email", ]; }
Examples
URL : /api/users
Content examples
For Users.
{ "data": [ { "id": 1, "first_name": "Joe", "last_name": "Bloggs", "email": "joe25@example.com" }, { "id": 2, "first_name": "Bob", "last_name": "Jonson", "email": "bob25@example.com" } ] }
Basic Usage
Retrieving All Models
$users = User::get();
Retrieving A Record By Id
$user = User::get('1');
Retrieving Records By Ida
$users = User::getMany(['1', '2']);
Wheres
$users = User::get()->where('first_name', 'Bob');
For more information check https://laravel.com/docs/collections
Inserts, updates and deletes
Saving a new model
User::post(['first_name' => 'Tim']);
Updating a model
To update a model, you may retrieve it, change an attribute, and use the put method.
$user = User::get('2'); $user->email = 'john@foo.com'; $user->put();
Or updating a model by its key
User::put('2', ['email' => 'john@foo.com']);
Deleting a model
To delete a model, simply call the delete method on the instance:
$user = User::get('1'); $user->delete();
Or deleting a model by its key:
User::delete('1');
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
- Efimov Aleksandr - Initial work - Sanchescom
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details