What is the difference between Models and Repository in laravel 5?

A model and a repository are not the same.

From the documentation - Models allow you to query for data in your tables, as well as insert new records into the table. What this is saying, is a Model opens access to a database table. It also allows you to relate to other models to pull out data without having to write individual queries.

A repository allows you to handle a Model without having to write massive queries inside of a controller. In turn, keeping the code tidier, modular and easier to debug should any errors arise.

You could use a repository in the following way:

public function makeNotification($class, $title, $message)
{
    $notification = new Notifications;
    ...
    $notification->save();
    return $notification->id;
}

public function notifyAdmin($class, $title, $message)
{
    $notification_id = $this->makeNotification($class, $title, $message);
    $users_roles = RolesToUsers::where('role_id', 1)->orWhere('role_id', 2)->get();
    $ids = $users_roles->pluck('user_id')->all();
    $ids = array_unique($ids);
    foreach($ids as $id) {
        UserNotifications::create([
            'user_id' => $id,
            'notification_id' => $notification_id,
            'read_status' => 0
        ]);
    }
}

And inside the controller:

protected $notification;

public function __construct(NotificationRepository $notification)
{
    $this->notification = $notification;
}

public function doAction()
{
    ...
    $this->notification->notifyAdmin('success', 'User Added', 'A new user joined the system');
    ...
}

The repository basically allows you to decouple your business logic from your database layer.

Whilst your eloquent model knows how to find all records for a given entity, your repository could wrap that in business-specific logic:

// Eloquent
$users = User::where('active', true)->get();

// Repository
$users = $this->userRepository->getAll(true);

class UserRepository {
    protected $model;

    public function __construct(User $model)
    {
        $this->model = $model;
    }

    public function getAll($active = false)
    {
        if ( $active === true )
        {
            return $this->model->where('active', true)->get();
        }

        return $this->model->all();
    }
}

This gives you testability and separation of concerns, your app is no longer tightly coupled to a particular implementation. In doing so, it becomes simpler to switch out the eloquent persistence for talking to a JSON API for example. This becomes easier still when you then you bind an interface to your controllers - something like UserRepositoryInterface - because you can update all uses of that interface just by updating one line in your service provider.

As with anything, though, it's up to you to determine if this is appropriate for your application. Repositories add another layer of complexity to your app that perhaps a small CRUD application won't actually need.

Tags:

Laravel