Gouda

Gouda

How to Create Repository Design Pattern with Laravel in 10 Steps

Subscribe to my newsletter and never miss my upcoming articles

1- Install Laravel Project

composer create-project laravel/laravel repositoryDesignPattern

2- Create Interfaces Folder in App/Http

3- Create Repositories Folder in App/Http

4- Try with Category CRUD Controller

php artisan make:controller CategoryController

5- Create Category Interface File


namespace App\Http\Interfaces;

interface CategoryInterface
{
    public function getCategories();

    public function categoryDetails();

    public function addCategory($request);

    public function deleteCategory();

    public function updateCategory($request);
}

6- Create Category Repository File

Note: in Repository File will implements from Interface.

namespace App\Http\Repositories;

use App\Http\Interfaces\CategoryInterface;

class CategoryRepository implements CategoryInterface
{

    public function getCategories()
    {
        // TODO: Implement getCategories() method.
    }

    public function categoryDetails()
    {
        // TODO: Implement categoryDetails() method.
    }

    public function addCategory($request)
    {
        // TODO: Implement addCategory() method.
    }

    public function deleteCategory()
    {
        // TODO: Implement deleteCategory() method.
    }

    public function updateCategory($request)
    {
        // TODO: Implement updateCategory() method.
    }
}

7- Make Repository Service Provider

php artisan make:provider RepositoryServiceProvider

8- Bind Interface with Repository in RepositoryServiceProvider Register


namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            'App\Http\Interfaces\CategoryRepositoryInterface',
            'App\Http\Repositories\CategoryRepository'
        );
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

9- Update Category Controller with CRUD functions Note** : in controller we need to use functions from interface so we can inject interface in construct like blew code:


namespace App\Http\Controllers;

use App\Http\Interfaces\CategoryInterface;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    private $categoryInterface;
    public function __construct(CategoryInterface $categoryInterface)
    {
        $this->categoryInterface = $categoryInterface;
    }

    public function getCategories()
    {
        $this->categoryInterface->getCategories();
    }

    public function categoryDetails()
    {
        $this->categoryInterface->categoryDetails();

    }

    public function addCategory(Request $request)
    {
        $this->categoryInterface->addCategory($request);

    }

    public function deleteCategory()
    {
        $this->categoryInterface->deleteCategory();
    }

    public function updateCategory(Request $request)
    {
        $this->categoryInterface->updateCategory($request);
    }

}

10- Finally Register Provider in config/app.php

  • set it in providers section with :
App\Providers\RepositoryServiceProvider::class

To be like :

``` 'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Bus\BusServiceProvider::class,
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Cookie\CookieServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Encryption\EncryptionServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Hashing\HashServiceProvider::class,
    Illuminate\Mail\MailServiceProvider::class,
    Illuminate\Notifications\NotificationServiceProvider::class,
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Pipeline\PipelineServiceProvider::class,
    Illuminate\Queue\QueueServiceProvider::class,
    Illuminate\Redis\RedisServiceProvider::class,
    Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    Illuminate\Session\SessionServiceProvider::class,
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,

    /*
     * Package Service Providers...
     */

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    // App\Providers\BroadcastServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    App\Providers\RepositoryServiceProvider::class,
],

```

Finally U Can Test Now U Can view code repo https://github.com/mohamedgouda98/repositoryDesignPattern

 
Share this