Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

laravel

I want to know what is the difference between Models and Repository in laravel 5. Both are same or not and what are benefits of repository.

like image 447
zikria-bashir Avatar asked Aug 09 '17 12:08

zikria-bashir


People also ask

What is repository in Laravel?

A repository can be defined as a layer of abstraction between the domain and data mapping layers, one that provides an avenue of mediation between both, via a collection-like interface for accessing domain objects.

What are models in Laravel?

Laravel Create Model is an MVC based PHP system. In the MVC architecture, 'M' stands for 'Model'. A model is used as a way for questioning data to and from the table within the database. Laravel gives a basic way to do that using Eloquent ORM where each table incorporates a Model to interact with it.

Why we use repositories in Laravel?

The main idea of using Repository Pattern in a Laravel application is to create a bridge between models and controllers. In other words, to decouple the hard dependencies of models from the controllers. The model should not be responsible for communicating with or extracting data from the database.

What is the difference between service and repository?

@Service annotation is used with classes that provide some business functionalities. @Repository Annotation is used to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects. @Service Annotation is a specialization of @Component Annotation.


1 Answers

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.

like image 175
samtax01 Avatar answered Oct 09 '22 11:10

samtax01