madeweb / eloquent-api
An Eloquent model with support for API, is a wrapper for use endpoints how to model entities
Requires
- illuminate/database: ^5.8|^6.0
This package is auto-updated.
Last update: 2025-04-29 01:02:57 UTC
README
This package allow use models and relationships using resources of API how to entities.
Installation
composer require madeweb/eloquent-api
Generate model wrapper for call to api
php artisan make:model-api Model --api=API\\NameOfAPI
Generate model wrapper for authentication with passport
php artisan make:model-api Model --api=API\\NameOfAPI --auth
Configure
Use fillable in models
Is very important setup the fillable because this variable allow recognize the fields will have your model.
protected $fillable = ['uuid', 'name', 'last_name', 'phone', 'document_type', 'document_number', 'birthdate', 'civil_status', 'gender', 'address_uuid', 'user_uuid'];
Relationships
For prepare relations between model is required use this syntax. For example for the person model the order of parameters is class belongs, key for identify relationship, common key or field in both models, and method of API for connecting.
public function user() { return $this->relationship(User::class, 'user', $this->user_uuid, 'find'); }
Methods Basics
How it is an api, it is necessary to define own functions to call basic methods that exist in eloquent. For example, in this function, the showPerson method must exist in the API.
public function find($uuid) { $response = $this->connection->showPerson($uuid); return $this->prepareResponse($response); }