Throughout this Laravel repository pattern example tutorial we will show you how to use repository pattern in laravel application. The repository pattern is intended to create an abstraction layer between the data access layer and the business logic layer of an application. It is a data access pattern that prompts a more loosely coupled approach to data access.
Repository pattern is that it allows us to use the Principle of Dependency Inversion (or code to abstractions, not concretions). This makes our code more robust to changes, such as if a decision was made later on to switch to a data source that isn’t supported by Eloquent.
We create the data access logic in a separate class, or set of classes, called a repository with the responsibility of persisting the application’s business model. Let’s start how to use repository pattern and whats the changes come in you controller and model for better use in laravel application.
Step 1: Create Repository File
First you need to create a new file app/Repositories/Repository.php inside the app, next open the app/Repositories/Repository.php file and update the below code on it;
<?php
namespace App\Repositories;
use Illuminate\Database\Eloquent\Model;
class Repository implements RepositoryInterface
{
// model property on class instances
protected $model;
// Constructor to bind model to repo
public function __construct(Model $model)
{
$this->model = $model;
}
// Get all instances of model
public function all()
{
return $this->model->all();
}
// create a new record in the database
public function create(array $data)
{
return $this->model->create($data);
}
// update record in the database
public function update(array $data, $id)
{
$record = $this->find($id);
return $record->update($data);
}
// remove record from the database
public function delete($id)
{
return $this->model->destroy($id);
}
// show the record with the given id
public function show($id)
{
return $this->model-findOrFail($id);
}
// Get the associated model
public function getModel()
{
return $this->model;
}
// Set the associated model
public function setModel($model)
{
$this->model = $model;
return $this;
}
// Eager load database relationships
public function with($relations)
{
return $this->model->with($relations);
}
}
Step 2: Create Repository Interface File
Next, create an another file named as RepositoryInterface.php insdie the app/Repoositories directory for repository interface. Now open the newly created app\Repositories\RepositoryInterface.php file and update the below code on it;
<?php namespace App\Repositories;
interface RepositoryInterface
{
public function all();
public function create(array $data);
public function update(array $data, $id);
public function delete($id);
public function show($id);
}
Step 3: Use Repository in Controller
Where you call model there you can use repository pattern just like below. We have use the repository and set the model in constructor just like below.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\Repository;
use App\Models\Post;
class PostController extends Controller
{
// space that we can use the repository from
protected $model;
public function __construct(Post $post)
{
// set the model
$this->model = new Repository($post);
}
public function index()
{
return $this->model->all();
}
public function store(Request $request)
{
return $this->model->create($request->all());
}
public function show($id)
{
return $this->model->show($id);
}
public function update(Request $request, $id)
{
$this->model->update($request->all()), $id);
}
public function destroy($id)
{
return $this->model->delete($id);
}
}
Note: If you are calling any methods and the method not in repository then you can get just using ‘getModel’ just like below.
public function index()
{
$posts = $this->post->getModel()->orderBy('id', 'desc')->get();
return response()->json($posts);
}
So, How to Use Repository Pattern in Laravel Application Step by Step tutorial is completed, Hope you enjoy to leaning new things. For more interesting update’s follow us on twitter.