dovutuan/laracom

Base repository and caching data

1.0.2 2023-11-08 10:20 UTC

This package is auto-updated.

Last update: 2024-05-09 12:15:08 UTC


README

Laravel Repositories is used to abstract the data layer, making our application more flexible to maintain.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Installation

Composer

Execute the following command to get the latest version of the package:

composer require dovutuan/laracom

Laravel

Publish Configuration

php artisan vendor:publish --tag=laracom

Methods

Dovutuan\Laracom\RepositoryInterface

Usage

Create a Repository

php artisan make:repository UserRepository

Create a Repository and Service

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;
    }
}

Create a Service

php artisan make:service UserService

Create a Service and Repository

php artisan make:service UserService --repo
<?php

namespace App\Services;

use App\Repositories\UserRepository;

class UserService
{
    public function __construct(private readonly UserRepository $userRepository)
    {
    }
}

Declare key search repository

<?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;
    }
}

Types of operators

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 = '!{}';

Types of boolean

const OPERATOR_BOOLEAN_AND = 'and';
const OPERATOR_BOOLEAN_OR = 'or';

Use methods

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]);