Simplify Laravel Database Access with Eloquent Repository: A Complete Guide

Hamid Ghorashi
4 min readFeb 18, 2023

If you’re working on a Laravel project and looking for a simple and efficient way to organize your application’s data access layer, the jetcod/eloquent-repository package is definitely worth considering. This library provides a clean and testable implementation of the repository pattern for Eloquent, Laravel’s built-in ORM.

One of the standout features of jetcod/eloquent-repository is its compatibility with the latest version of PHP, including PHP 8.0 and later. This means that you can take advantage of all the latest language features and improvements while building your Laravel application.

The jetcod/eloquent-repository package is also designed to be easy to use, with a consistent and intuitive interface for querying and manipulating your application’s data. It includes methods for common operations like finding, creating, updating, and deleting records, making it a powerful tool for working with Eloquent models.

To install the package, simply open up a terminal or command prompt, navigate to your Laravel project directory, and run the following command:

composer require jetcod/eloquent-repository

This will download and install the package, along with any dependencies it requires.

Once you’ve installed the package, you can start using it in your Laravel application. To generate a repository class for a model using the jetcod/eloquent-repository package, you can use the make:repository Artisan command. Here’s an example of how you might use this command to generate a repository class for a User model:

php artisan make:repository --model=User UserRepository

This command will generate a new repository class in the app/Repositories directory with the name UserRepository. The new class will extend the BaseRepository class provided by the package, and implements abstract method called getModelName() to specify the model class.

This can be a handy shortcut for quickly creating repository classes for your Laravel models, and can save you time and effort in setting up your application’s data access layer. Here’s an example of how you might create a repository class for a User model:

<?php

namespace App\Repositories\UserRepository;

use App\Models\User;
use Jetcod\LaravelRepository\BaseRepository;

class UserRepository extends BaseRepository
{
protected function getModelName()
{
return User::class;
}
}

In this example, we’re extending the BaseRepository class provided by jetcod/eloquent-repository and specifying the User model as the model class. This creates a simple and consistent interface for accessing the User data in our application.

To use the repository, we can simply inject it to your controllers or services. Here is an example of getting it to use in UserService class:

<?php

namespace App\Services;

use App\Repositories\UserRepository;

class UserService
{
protected $userRepository;

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

public function register(array $attributes): User
{
return $this->userRepository->create($attributes);
}
}

Here the create method of the repository class is called and it returns the created User model.

One of the key points it that implementing the repository pattern in your project can provide you with a powerful and flexible way to add custom events and hooks to your data access layer.

When you use a repository class to interact with your database, you have complete control over how your application’s data is created, updated, and deleted. This means that you can easily add custom logic to your repository methods to trigger events or perform other actions when certain data operations are performed.

For example, let’s say you want to trigger an email notification whenever a new user is created in your application. You could add a custom method to your UserRepository class to handle this logic:

<?php

namespace App\Repositories\UserRepository;

use App\Models\User;
use Jetcod\LaravelRepository\BaseRepository;

class UserRepository extends BaseRepository
{
protected function getModelName()
{
return User::class;
}

public function create(array $data): User
{
$user = parent::create($data);

// Send email notification to user
Mail::to($user->email)->send(new NewUserWelcomeEmail($user));

return $user;
}
}

In this example, we’ve added a custom create() method to our UserRepository that first creates the new user using the parent create() method provided by the BaseRepository. We then send an email notification to the user using Laravel’s built-in Mail class.

This is just one example of how you can use the repository pattern to add custom events and hooks to your data access layer. By using a repository class to encapsulate your data access logic, you can easily add custom functionality like this without cluttering up your application’s controllers or models.

Overall, using a repository pattern in your Laravel application can provide you with a powerful and flexible way to add custom events and hooks to your data access layer, making it easier to add advanced functionality and automation to your application.

--

--