Simplified Repository pattern implementation in Laravel

Overview

Laravository - Repository Pattern for Laravel

Simplified Repository pattern implementation in Laravel.

Requirement

  • Laravel 8.x

Installation

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

composer require akunbeben/laravository

Execute the following command to create the RepositoryServiceProvider:

php artisan repository:provider

Go to the config/app.php and add this App\Providers\RepositoryServiceProvider::class to your providers config:

The RepositoryServiceProvider will work as Dependency Injection.

'providers' => [
  ...
  App\Providers\RepositoryServiceProvider::class,
],

Methods

Akunbeben\Laravository\Repositories\Interfaces

  • getAll();
  • getById($id, $relations = null)
  • create($attributes)
  • update($id, $attributes)
  • delete($id)

Usage

Create a new Repository

To create repository, you just need to run like this:

php artisan make:repository User

You can also add -m or --model option to generate the Model:

php artisan make:repository User -m

So you don't need to create Model manually.

You can find out the generated files under the App\Repositories\ folder:

  • Repository: App\Repositories\Eloquent\
  • Interface: App\Repositories\Interfaces\

UserRepository.php

namespace App\Repositories\Eloquent;

use Akunbeben\Laravository\Repositories\Eloquent\BaseRepository;

use App\Repositories\Interfaces\UserRepositoryInterface;
use App\Models\User;

class UserRepository extends BaseRepository implements UserRepositoryInterface
{
  protected $model;

  /**
   * Your model to use the Eloquent
   * 
   * @param User $model
   */
  public function __construct(User $model)
  {
    $this->model = $model;
  }
}

UserRepositoryInterface.php

namespace App\Repositories\Interfaces;

interface UserRepositoryInterface
{
    
}

After you created the repositories. you need to register your Class and Interface in the RepositoryServiceProvider.php

...
class RepositoryServiceProvider extends ServiceProvider
{
  ...
  public function register()
  {
    ...
    $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
  }
}

Now you can implement it to your Controller. Like this example below:

namespace App\Http\Controllers;

use App\Repositories\Interfaces\UserRepositoryInterface;

class UserController extends Controller
{
  protected $userRepository;

  public function __construct(UserRepositoryInterface $userRepository) {
    $this->userRepository = $userRepository;
  }
}

So your Controller will much cleaner and much shorter.

class UserController extends Controller
{
  protected $userRepository;

  public function __construct(UserRepositoryInterface $userRepository) {
    $this->userRepository = $userRepository;
  }

  ...

  public function store(SomeFormRequest $request)
  {
    return $this->userRepository->create($request->validated());
  }
}
You might also like...
LaraAdmin is a Open source Laravel Admin Panel / CMS which can be used as Admin Backend, Data Management Tool or CRM boilerplate for Laravel with features like Advanced CRUD Generation, Module Manager, Backups and many more.
LaraAdmin is a Open source Laravel Admin Panel / CMS which can be used as Admin Backend, Data Management Tool or CRM boilerplate for Laravel with features like Advanced CRUD Generation, Module Manager, Backups and many more.

LaraAdmin 1.0 LaraAdmin is a Open source CRM for quick-start Admin based applications with features like Advanced CRUD Generation, Schema Manager and

The Laravel Boilerplate Project - https://laravel-boilerplate.com

Laravel Boilerplate (Current: Laravel 8.*) (Demo) Demo Credentials Admin: [email protected] Password: secret User: [email protected] Password: secret Offici

A Laravel 5 package that switchs default Laravel scaffolding/boilerplate to AdminLTE template and Pratt Landing Page with Bootstrap 3.0

AdminLTE template Laravel package A Laravel package that switch default Laravel scaffolding / boilerplate to AdminLTE template with Bootstrap 3.0 and

A Laravel Admin Panel (Laravel Version : 6.0)
A Laravel Admin Panel (Laravel Version : 6.0)

Laravel Admin Panel (Current: Laravel 7.*) Introduction Laravel Admin Panel provides you with a massive head start on any size web application. It com

Laravel Vue SPA, Bulma themed. For demo login use `admin@laravel-enso.com` & `password` -
Laravel Vue SPA, Bulma themed. For demo login use `[email protected]` & `password` -

Laravel Enso Hit the ground running when building your new Laravel SPA project with boilerplate and extra functionality out of the box! click on the p

High scalable boilerplate for Laravel - Vue using laravel-mix.

Why use this ? This boilerplate make developer easier to make monolith Laravel project which integrated with Vue.js and vue-router as default front-en

Gestor de Contraseñas basado en Laravel 8 + PHP 8 + MySQL 8. Self-hosted Password Manager based on Laravel 8 + PHP 8 + MySQL 8.
Gestor de Contraseñas basado en Laravel 8 + PHP 8 + MySQL 8. Self-hosted Password Manager based on Laravel 8 + PHP 8 + MySQL 8.

English Gestor de Contraseñas Esta aplicación permite una gestión completa de contraseñas para múltiples tipos de servicios (web, ssh, teléfonos, wifi

Laravel Vue SPA, Bulma themed. For demo login use `admin@laravel-enso.com` & `password` -
Laravel Vue SPA, Bulma themed. For demo login use `[email protected]` & `password` -

Laravel Enso Hit the ground running when building your new Laravel SPA project with boilerplate and extra functionality out of the box! click on the p

laravel/ui with auth scaffolding for Laravel 8

Legacy UI Presets with Auth scaffolding for Laravel 8 Introduction This project brings old Auth scaffolding to Laravel 8 for projects that cannot migr

Releases(1.0.3)
Owner
Benny Rahmat
I'm a Software Engineer | reach me at [email protected]
Benny Rahmat
Github repository dedicated for my YT tutorial which shows how to use Sessions in Laravel

Sessions in Laravel The following documentation is based on my Laravel Sessions for Beginners tutorial we’re going to cover the basics of sessions in

Code With Dary 11 Nov 25, 2022
An open source blog, made using Laravel, Inertia.js, and React.js. Also a learning repository.

Blog An open source Laravel-based blog. Still in progress, will be updated regularly as I wrote articles on my blog. Configurations Shared-hosting, an

Aghits Nidallah 8 Dec 6, 2022
Template repository for new Blade Icons packages.

Blade Icons Template This is a template repository for new icon packages for Blade Icons.

Blade UI Kit 11 Nov 30, 2022
Template for repository helper, library - Basic, Simple and Lightweight

Template start helper, library Template for repository helper, library - Basic, Simple and Lightweight Use this Template First, you can Use this templ

Hung Nguyen 2 Jun 17, 2022
This is a repository for anyone wishing to contribute to HacktoberFest 2021

HacktoberFest 2021 Status IMPORTANT NOTICE : We have stopped accepting PRs for DSA Please Try to add more projects/apps/webapp instead of just DSA cod

Viral Vaghela 51 Nov 6, 2022
This repository is pre-configured, clean and empty skeleton for creating a new projects using Kraken Framework.

Kraken Application Skeleton Note: This repository contains pre-configured application skeleton for fast creation of new projects with Kraken Framework

Kraken 79 Aug 6, 2022
This template repository is a boilerplate setup for a PHP application

PHP_Docker Repository Template What is this ? This template repository is a boilerplate setup for a PHP application. Its current version ships with PH

Kevin Richard 0 Jul 16, 2022
An implementation of Solder in Laravel's Lumen Framework.

LumenSolder What is LumenSolder? An implementation of Solder in Laravel's Lumen Framework. LumenSolder is an application to help compliment and scale

Technic Pack 3 Sep 16, 2020
A Laravel Starter Kit for Laravel. Built with Laravel 8.

Laravel Get Started Project Laravel Get Started Project is a basic crud app built with laravel 8. In this app a basic product crud created. Features i

Nazmul Hasan Robin 8 Nov 24, 2022