gkalmoukis/laravel-repositories

v0.0.3.2 2023-04-24 15:54 UTC

This package is auto-updated.

Last update: 2024-05-24 18:25:56 UTC


README

A simple package to use Repository Pattern approach for Eloquent models.

Repository pattern

Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer. Microsoft

Installation

Require the package using composer:

composer require gkalmoukis/laravel-repositories

Command and Configuration

To use this package, you need to have repository class bound to laravel model class . This package includes a command that make it easy to to create repository classes from command line . to create a new repository class, run the following command

php artisan make:repository Repository --model=DummyModel

Usage

The best way to use the repository classes via Dependency Injection through the controller classes . for example :

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\Repository;

class DummyController extends Controller {

	/**
     * create a new controller instance
     *
     * @param  \App\Repositories\Repository $repository
     * @return void
     */
    public function __construct(
        protected Repository $repository
    ) {}
}

And in that way one can already get a fully qualified repository class . Also to manually initiated :

$repository = new \App\Repositories\Repository(new DummyModel);