dilneiss / laravel-repository
Laravel Repository Package
v1.0.0
2022-02-09 00:58 UTC
Requires
- php: ^7.0|^8.0
- laravel/framework: ^5.5|^6.0|^7.0|^8.0|^9.0
This package is auto-updated.
Last update: 2024-11-09 06:57:27 UTC
README
Laravel Version Support
-
^5.5
^6.0
^7.0
^8.0
Php Version Support
-
^7.0
^8.0
Table of Contents
Installation
Composer
Execute the following command to get the latest version of the package:
composer require jetbox/laravel-repository
Repository Install
php artisan repository:install
Create Repository
Create a new Eloquent model repository class
php artisan make:repository UserRepository
Methods
JetBox\Repositories\Contracts\RepositoryInterface
- get($columns = ['*'], $take = false, $pagination = false, $where = false);
- all($columns = ['*']);
- take($take, $columns = ['*']);
- paginate($perPage = false, $columns = ['*']);
- withPaginate($relations, $columns = ['*'], $paginate = 15);
- simplePaginate($perPage = false, $columns = ['*']);
- limit($take, $columns = ['*']);
- find($id, $columns = ['*']);
- findMany($ids, $columns = ['*']);
- findOrFail($id, $columns = ['*']);
- first($columns = ['*']);
- firstOrFail($columns = ['*']);
- where($column, $value = null, $columns = ['*']);
- whereOrFail($column, $value = null, $columns = ['*']);
- whereAll($column, $value = null, $columns = ['*']);
- whereWithAll($column, $value = null, $relations, $columns = ['*']);
- whereBetween($column, $value = [], $columns = ['*']);
- with($relations, $columns = ['*']);
- withCount($relations, $columns = ['*']);
- pluck($column, $key = null);
- create(array $attributes);
- forceCreate(array $attributes);
- update(array $attributes, $model, bool $tap = false, bool $forceFill = false);
- updateForce(array $attributes, $model, bool $tap = false);
- delete($model, bool $tap = false, bool $forceDelete = false);
- forceDelete($model, bool $tap = false);
- querySortable(string $orderByColumn, string $orderByDirection)
Helpers
- lLog(string $message, string $log = 'info', array $context = [], string $disk = null)
- is_json(string $str, bool $returnData = false)
- currentUser(): ?Authenticatable
- numberFormatShort($n, int $precision = 2)
EnvironmentTrait
use JetBox\Repositories\Traits\EnvironmentTrait
- changeEnvironmentVariable(string $key, $value): void
- environmentVariableAllUpdate(array $data): void
File Facade
- JetBoxFile::save(string $path, object $file, string $fileName = null, array $options = [])
- JetBoxFile::delete(Model $model, string $field, string $path)
- JetBoxFile::numberFormatSizeUnits(int $sizeInBytes)
Constants
- AppConstants::permissions(): array
- AppConstants::roles(): array
- AppConstants::status(): array
namespace App\Constants; use JetBox\Repositories\Constants\AppConstants as BaseAppConstants; final class AppConstants extends BaseAppConstants { const ROLE_VISITOR = 'visitor'; const ROLE_EDITOR = 'editor'; const PERMISSION_VIEW_BLOG = 'view_blog'; }
Usage
Create a Repository
Recommended This Shorter
Laravel
^5.7
^6.0
^7.0
^8.0
if your model is not linked to the repository auto, you can override the$model
property of your repository
namespace App\Repositories; class UserRepository extends AbstractRepository { }
Or
Laravel
^5.2
<=5.6
override the$model
property
namespace App\Repositories; use App\Models\User; class UserRepository extends AbstractRepository { /** * @var string */ protected $model = User::class; /** * Global OrderBy Column * @var string */ public $orderByColumn = 'created_at'; /** * Global OrderBy Direction * @var string */ public $orderByDirection = 'desc'; }
Use methods
namespace App\Http\Controllers; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; use Illuminate\View\View; use App\Repositories\UserRepository as UserR; class UserController extends BaseController { /** * @var $users */ protected $users; /** * UserController constructor. * @param UserR $users */ public function __construct(UserR $users) { $this->users = $users; } /** * @return Application|Factory|View */ public function index() { $users = $this->users->all(); return view('users', compact('users')); } }
Find all results in Repository
$users = $this->users->all();
Find by result by id
$user = $this->users->find($id);