1

I'm writing a new project with laravel 5.1, and I want to use the repository pattern, but I can't figure out what is the best way of doing that.

I was thinking about:

public function save(User $user) { $user->save(); } public function find($id) { return User::find($id); } 

And then

$user = new User(); $user->email = '[email protected]'; $userRepo->save($user); 

That way I work with the model as DTO, and it's super easy and comfortable, but then I will be depend on Eloquent.

So I was thinking of using an array instead of model, like that:

public function save(array $user) { User::save($user); } public function find($id) { return User::find($id)->toArray(); } 

But then it will be much less comfortable to use.

Can you explain me what is the best way of using repository in Laravel so I will be able to change data source easily in the future, but it will still be comfortable to use this pattern?

4
  • What's the point of a repository if you put that logic in the model? Commented Nov 23, 2015 at 22:46
  • @Federico I use eloquent in this repository, but in other repository that implements the same interface I will use something else, for example a file source, but I can still use the model as a DTO. Commented Nov 23, 2015 at 22:50
  • @Federico And this code is in the repository file, not in the model, if it wasn't clear Commented Nov 23, 2015 at 22:57
  • My answer to same question: stackoverflow.com/a/79173292/1429432 Commented Nov 9, 2024 at 17:16

1 Answer 1

1

I have it set up as follows, using your example:

<?php namespace Repositories; use Models\User; class UserEloquentRespository implements UserInterface { protected $user; public function __construct(User $user) { $this->user = $user; } public function create($input) { $this->user->create($input); } } 

Then in my controller

Laravel 4

<?php namespace Controllers; use Repositories\UserInterface as User; class UsersController extends \BaseController { protected $user; public function __construct(User $user) { $this->user = $user; } public function store() { $input = \Input::all(); // validation here // now create your user $user = $this->user->create($input); if ($user) { // redirect or do whatever you do on successful user creations } // Here you can do whatever you do if a user wasn't created for whatever reason } } 

Laravel 5

<?php namespace App\Http\Controllers; use Repositories\UserInterface as User; use App\Http\Requests\UserRequestForm; use App\Http\Controllers\Controller; class UsersController extends Controller { protected $user; public function __construct(User $user) { $this->user = $user; } public function store(UserRequestForm $request) { // Form request validates in middleware // now create your user $user = $this->user->create($request->input()); if ($user) { // redirect or do whatever you do on successful user creations } // Here you can do whatever you do if a user wasn't created for whatever reason } } 

Lastly, don't forget to bind your interface to your eloquent repository as a service provider.

 App::bind( 'Repositories\UserRepositoryInterface', 'Repositories\Eloquent\UserEloquentRepository' ); 

Now, whenever you need to change your implementation just change the binding to whatever new class implements your interface.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.