| Package Data | |
|---|---|
| Maintainer Username: | YaangVu | 
| Maintainer Contact: | yaangvu@gmail.com (Yaang Vu) | 
| Package Create Date: | 2021-04-18 | 
| Package Last Update: | 2025-02-28 | 
| Language: | PHP | 
| License: | MIT | 
| Last Refreshed: | 2025-10-29 03:01:24 | 
| Package Statistics | |
|---|---|
| Total Downloads: | 11,346 | 
| Monthly Downloads: | 117 | 
| Daily Downloads: | 4 | 
| Total Stars: | 7 | 
| Total Watchers: | 1 | 
| Total Forks: | 1 | 
| Total Open Issues: | 1 | 
This base will help to create simple API (CRUD) for 1 specific entity, such as User
composer require yaangvu/laravel-base
.
├── src
│   ├── Constants
│   │   ├── DataCastConstant.php
│   │   └── OperatorConstant.php
│   ├── Controllers
│   │   └── BaseController.php
│   ├── Exceptions
│   │   ├── BadRequestException.php
│   │   ├── BaseException.php
│   │   ├── ForbiddenException.php
│   │   ├── GatewayTimeOutException.php
│   │   ├── Handler.php
│   │   ├── NotFoundException.php
│   │   ├── SystemException.php
│   │   └── UnauthorizedException.php
│   ├── Helpers
│   │   ├── FileHelper.php
│   │   ├── LocalFileHelper.php
│   │   ├── QueryHelper.php
│   │   └── RouterHelper.php
│   ├── LaravelBaseServiceProvider.php
│   ├── Services
│   │   ├── BaseServiceInterface.php
│   │   └── impl
│   └── config
│       └── laravel-base.php
use YaangVu\LaravelBase\Helpers\RouterHelper;
RouterHelper::resource($router, '/users', 'UserController');
use App\Models\User;
use YaangVu\LaravelBase\Services\impl\BaseService;
class UserService extends BaseService
{
    function createModel(): void
    {
        $this->model = new User();
    }
}
use App\Services\UserService;
use YaangVu\LaravelBase\Controllers\BaseController;
class UserController extends BaseController
{
    public function __construct()
    {
        $this->service = new UserService();
        parent::__construct();
    }
}
To insert or update an entity, you must define columns can give data
class User extends Model
{
    protected $fillable
        = [
            'username', 'email', 'password', 'first_name', 'last_name'
        ];
}
$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 admin
name__~ = super  ---->  name like %super%
age__gt = 18     ---->  age gather than 18
Request 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:
preAdd
postAdd
preUpdate
postUpdate
preDelete
postDelete
preGet
postGet
preGetAll
postGetAll
use YaangVu\LaravelBase\Helpers\LocalFileHelper;
$fileHelper = new LocalFileHelper();
    if ($request->video)
            $videoPath = $fileHelper->upload($request, 'video', 'university/video');