This library provides tools and interfaces for working with REST API and using Laravel Models and Collections.
Require this package, with Composer, in the root directory of your project.
$ composer require sanchescom/laravel-rest
After updating composer, add the ServiceProvider to the providers array in config/app.php
'providers' => [
...
Sanchescom\Rest\RestServiceProvider::class,
],
After updating composer add the following lines to register provider in bootstrap/app.php
$app->register(Sanchescom\Rest\RestServiceProvider::class);
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',
],
],
],
],
];
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",
];
}
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"
}
]
}
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
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');
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
- Efimov Aleksandr - Initial work - Sanchescom
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details