Getting Started with Repository Pattern in Laravel using Inheritance and Dependency Injection (ok)

https://dev.to/carlomigueldy/getting-started-with-repository-pattern-in-laravel-using-inheritance-and-dependency-injection-2ohe

C:\xampp\htdocs\lv\app\Repository\Eloquent\BaseRepository.php

<?php
namespace App\Repository\Eloquent;
use App\Repository\EloquentRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class BaseRepository implements EloquentRepositoryInterface {
  /**
   * @var Model
   */
  protected $model;
  /**
   * BaseRepository constructor.
   *
   * @param Model $model
   */
  public function __construct(Model $model) {
    $this->model = $model;
  }
  /**
   * @param array $columns
   * @param array $relations
   * @return Collection
   */
  public function all(array $columns = ['*'], array $relations = []): Collection {
    return $this->model->with($relations)->get($columns);
  }
  /**
   * Get all trashed models.
   *
   * @return Collection
   */
  public function allTrashed(): Collection {
    return $this->model->onlyTrashed()->get();
  }
  /**
   * Find model by id.
   *
   * @param int $modelId
   * @param array $columns
   * @param array $relations
   * @param array $appends
   * @return Model
   */
  public function findById(
    int $modelId,
    array $columns = ['*'],
    array $relations = [],
    array $appends = []
  ):  ? Model {
    return $this->model->select($columns)->with($relations)->findOrFail($modelId)->append($appends);
  }
  /**
   * Find trashed model by id.
   *
   * @param int $modelId
   * @return Model
   */
  public function findTrashedById(int $modelId) :  ? Model {
    return $this->model->withTrashed()->findOrFail($modelId);
  }
  /**
   * Find only trashed model by id.
   *
   * @param int $modelId
   * @return Model
   */
  public function findOnlyTrashedById(int $modelId) :  ? Model {
    return $this->model->onlyTrashed()->findOrFail($modelId);
  }
  /**
   * Create a model.
   *
   * @param array $payload
   * @return Model
   */
  public function create(array $payload) :  ? Model{
    $model = $this->model->create($payload);
    return $model->fresh();
  }
  /**
   * Update existing model.
   *
   * @param int $modelId
   * @param array $payload
   * @return bool
   */
  public function update(int $modelId, array $payload) : bool{
    $model = $this->findById($modelId);
    return $model->update($payload);
  }
  /**
   * Delete model by id.
   *
   * @param int $modelId
   * @return bool
   */
  public function deleteById(int $modelId): bool {
    return $this->findById($modelId)->delete();
  }
  /**
   * Restore model by id.
   *
   * @param int $modelId
   * @return bool
   */
  public function restoreById(int $modelId): bool {
    return $this->findOnlyTrashedById($modelId)->restore();
  }
  /**
   * Permanently delete model by id.
   *
   * @param int $modelId
   * @return bool
   */
  public function permanentlyDeleteById(int $modelId): bool {
    return $this->findTrashedById($modelId)->forceDelete();
  }
}

C:\xampp\htdocs\lv\app\Repository\Eloquent\UserRepository.php

<?php
namespace App\Repository\Eloquent;
use App\Models\User;
use App\Repository\UserRepositoryInterface;
class UserRepository extends BaseRepository implements UserRepositoryInterface {
  /**
   * @var Model
   */
  protected $model;
  /**
   * BaseRepository constructor.
   *
   * @param Model $model
   */
  public function __construct(User $model) {
    $this->model = $model;
  }
}

C:\xampp\htdocs\lv\app\Repository\EloquentRepositoryInterface.php

<?php
namespace App\Repository;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
interface EloquentRepositoryInterface {
  /**
   * Get all models.
   *
   * @param array $columns
   * @param array $relations
   * @return Collection
   */
  public function all(array $columns = ['*'], array $relations = []): Collection;
  /**
   * Get all trashed models.
   *
   * @return Collection
   */
  public function allTrashed(): Collection;
  /**
   * Find model by id.
   *
   * @param int $modelId
   * @param array $columns
   * @param array $relations
   * @param array $appends
   * @return Model
   */
  public function findById(
    int $modelId,
    array $columns = ['*'],
    array $relations = [],
    array $appends = []
  ):  ? Model;
  /**
   * Find trashed model by id.
   *
   * @param int $modelId
   * @return Model
   */
  public function findTrashedById(int $modelId) :  ? Model;
  /**
   * Find only trashed model by id.
   *
   * @param int $modelId
   * @return Model
   */
  public function findOnlyTrashedById(int $modelId) :  ? Model;
  /**
   * Create a model.
   *
   * @param array $payload
   * @return Model
   */
  public function create(array $payload) :  ? Model;
  /**
   * Update existing model.
   *
   * @param int $modelId
   * @param array $payload
   * @return bool
   */
  public function update(int $modelId, array $payload) : bool;
  /**
   * Delete model by id.
   *
   * @param int $modelId
   * @return bool
   */
  public function deleteById(int $modelId): bool;
  /**
   * Restore model by id.
   *
   * @param int $modelId
   * @return bool
   */
  public function restoreById(int $modelId): bool;
  /**
   * Permanently delete model by id.
   *
   * @param int $modelId
   * @return bool
   */
  public function permanentlyDeleteById(int $modelId): bool;
}

C:\xampp\htdocs\lv\app\Repository\UserRepositoryInterface.php

<?php
namespace App\Repository;
interface UserRepositoryInterface extends EloquentRepositoryInterface {}

C:\xampp\htdocs\lv\app\Http\Controllers\UserController.php

<?php
namespace App\Http\Controllers;
use App\Repository\UserRepositoryInterface;
class UserController extends Controller {
  private $userRepository;
  public function __construct(UserRepositoryInterface $userRepository) {
    $this->userRepository = $userRepository;
  }
  public function index() {
    return response()->json($this->userRepository->all(), 200);
  }
}

C:\xampp\htdocs\lv\routes\api.php

<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});
Route::get('users', [UserController::class, 'index']);

C:\xampp\htdocs\lv\app\Providers\RepositoryServiceProvider.php

<?php
namespace App\Providers;
use App\Repository\EloquentRepositoryInterface;
use App\Repository\Eloquent\BaseRepository;
use App\Repository\Eloquent\UserRepository;
use App\Repository\UserRepositoryInterface;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider {
  /**
   * Register services.
   *
   * @return void
   */
  public function register() {
    $this->app->bind(EloquentRepositoryInterface::class, BaseRepository::class);
    $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
  }
  /**
   * Bootstrap services.
   *
   * @return void
   */
  public function boot() {
    //
  }
}

C:\xampp\htdocs\lv\app\Providers\AppServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider {
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register() {
    $this->app->register(RepositoryServiceProvider::class);
  }
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot() {
    //
  }
}

Last updated