yaangvu/laravel-base is a Laravel package for laravel api resource base.
It currently has 6 GitHub stars and 11.638 downloads on Packagist (latest version v4.1.3).
Install it with composer require yaangvu/laravel-base.
Discover more Laravel packages by yaangvu
or browse all Laravel packages to compare alternatives.
Last updated
This base will help to create simple API (CRUD) for 1 specific entity
composer require yaangvu/laravel-base
Publish configuration file and Base Classes
php artisan vendor:publish --provider="YaangVu\LaravelBase\Providers\BaseServiceProvider"
cp vendor/yaangvu/laravel-base/src/config/laravel-base.php config/laravel-base.php
mkdir -p app/Base
cp vendor/yaangvu/laravel-base/src/Base/Publish/Controller.php app/Base/Controller.php
cp vendor/yaangvu/laravel-base/src/Base/Publish/Service.php app/Base/Service.php
If you want to use Generator Command, Add the following class to the providers array in config/app.php:
YaangVu\LaravelBase\Provider\GeneratorServiceProvider::class,
If you want to manually load it only in non-production environments, instead you can add this to
your AppServiceProvider with the register() method:
public function register()
{
if ($this->app->isLocal()) {
$this->app->register(\YaangVu\LaravelBase\Provider\GeneratorServiceProvider::class);
}
// ...
}
php artisan yaangvu:base Post <option>
Option:
├── app
│ ├── Domains
│ │ └── Post
│ │ ├── Controllers
│ │ │ └── PostController.php
│ │ ├── Models
│ │ │ └── Post.php
│ │ └── Services
│ │ └── PostService.php
Route::base('/posts', \App\Domains\Post\Controllers\PostController::class);
$operators
= [
'__gt' => OperatorConstant::GT, // Greater than
'__ge' => OperatorConstant::GE, // Greater than or equal
'__lt' => OperatorConstant::LT, // Less than
'__le' => OperatorConstant::LE, // Less than or equal
'__~' => OperatorConstant::LIKE // Like
];
{param-name}{operator} = {value}
username = admin ----> username equal adminname__~ = super ----> name like %super%age__gt = 18 ----> age gather than 18Request to query user with username=admin and name LIKE %super% and age > 18
curl --location --request GET 'http://localhost:8000/api/v1/users?username=admin&name__~=super&age__gt=18'
Support full Laravel validation: Validation
class UserService extends BaseService
{
public function storeRequestValidate(object $request, array $rules = []): bool|array
{
$rules = [
'username' => 'required|max:255|unique:users',
];
return parent::storeRequestValidate($request, $rules);
}
}
Support full Laravel validation: Validation
class UserService extends BaseService
{
public function updateRequestValidate(int|string $id, object $request, array $rules = []): bool|array
{
$rules = [
'username' => 'required|max:255|unique:users,id',
];
return parent::updateRequestValidate($id, $request, $rules);
}
}
It supports these observe function:
function postAdd()function postUpdate()function postDelete()function postGet()function postGetAll()If you want to cache data when create update select, implement ShouldCache interface
class UserService extends BaseService implements \YaangVu\LaravelBase\Base\Contract\ShouldCache
{}