Laravel Repositories is used to abstract the data layer, making our application more flexible to maintain.
Execute the following command to get the latest version of the package:
composer require dovutuan/laracom
Publish Configuration
php artisan vendor:publish --tag=laracom
php artisan make:repository UserRepository
php artisan make:repository UserRepository --ser
<?php
namespace App\Repositories;
use App\Models\User;
use Dovutuan\Laracom\DomRepository\BaseRepository;
class UserRepository extends BaseRepository
{
public function model(): string
{
return User::class;
}
}
php artisan make:service UserService
php artisan make:service UserService --repo
<?php
namespace App\Services;
use App\Repositories\UserRepository;
class UserService
{
public function __construct(private readonly UserRepository $userRepository)
{
}
}
<?php
namespace App\Repositories;
use App\Models\User;
use Dovutuan\Laracom\DomRepository\BaseRepository;
class UserRepository extends BaseRepository
{
protected array $base_search = [
'id' => [
['column' => 'id', 'operator' => OPERATOR_EQUAL]
],
'keyword' => [
['column' => 'name', 'operator' => OPERATOR_LIKE, 'boolean' => OPERATOR_BOOLEAN_OR],
['column' => 'email', 'operator' => OPERATOR_LIKE, 'boolean' => OPERATOR_BOOLEAN_AND],
],
'name' => [
['column' => 'name', 'operator' => OPERATOR_LIKE],
]
];
public function model(): string
{
return User::class;
}
}
const OPERATOR_EQUAL = '=';
const OPERATOR_NOT_EQUAL = '<>';
const OPERATOR_LIKE = '%%';
const OPERATOR_BEFORE_LIKE = '%_';
const OPERATOR_AFTER_LIKE = '_%';
const OPERATOR_NOT_LIKE = '!%%';
const OPERATOR_GREATER = '>';
const OPERATOR_GREATER_EQUAL = '>=';
const OPERATOR_LESS = '<';
const OPERATOR_LES_EQUAL = '<=';
const OPERATOR_IN = 'in';
const OPERATOR_NOT_IN = '!in';
const OPERATOR_NULL = 'null';
const OPERATOR_NOT_NULL = '!null';
const OPERATOR_DATE = 'date';
const OPERATOR_DATE_NOT_EQUAL = '!date';
const OPERATOR_DATE_GREATER = '>date';
const OPERATOR_DATE_GREATER_EQUAL = '>=date';
const OPERATOR_DATE_LESS = '<date';
const OPERATOR_DATE_LESS_EQUAL = '<=date';
const OPERATOR_JSON = '{}';
const OPERATOR_JSON_NOT_CONTAIN = '!{}';
const OPERATOR_BOOLEAN_AND = 'and';
const OPERATOR_BOOLEAN_OR = 'or';
Find result by id in Repository
$user = $this->userRepository->find(123);
Find result by conditions in Repository
$user = $this->userRepository->findByConditions(['id' => 123]);
Create new entry in Repository
$user = $this->userRepository->create(Input::all());
Update entry in Repository
$user = $this->userRepository->update(123, Input::all());
Update entry by conditions in Repository
$user = $this->userRepository->updateByConditions(['id' => 123], Input::all());
Delete entry in Repository
$user = $this->userRepository->delete(123);
Delete entry by conditions in Repository
$user = $this->userRepository->deleteByConditions(['id' => 123]);
Count entry by conditions in Repository
$user = $this->userRepository->count(['id' => 123]);
Paginate entry by conditions in Repository
$user = $this->userRepository->paginate(['id' => 123]);
All entry by conditions in Repository
$user = $this->userRepository->all(['id' => 123]);
Insert entry by conditions in Repository
$user = $this->userRepository->inserrt([['name' => 'Hello'], ['name' => 'Hi']]);
Update or create entry by conditions in Repository
$user = $this->userRepository->updateOrCreate(['id' => 123], ['name' => 'Hello']);
Upsert entry by conditions in Repository
$user = $this->userRepository->update(['id' => 123, 'name' => 'Hello'], ['id'], ['name']);
All and pluck entry by conditions in Repository
$user = $this->userRepository->allAndPluck('name', 'id', ['id' => 123]);