HTMHell
HTMHell

Reputation: 6006

Laravel Repository Pattern Best Practice

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?

Upvotes: 1

Views: 2471

Answers (1)

Joshua
Joshua

Reputation: 791

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.

Upvotes: 1

Related Questions