This package allow use models and relationships using resources of API how to entities.
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
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'];
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');
}
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);
}