Today now in this post i want to share with you a tutorial of how to create Repository Pattern in Laravel 8 application. Generally we are getting data directly from the model, I mean most of we are use just MVC. But if we have any big application then it is better way if we use Repository Pattern. Here I will give you few steps to create a Repository Pattern. Normally we are use directly from Model but if we make Repository Pattern for our every module then it is good way to develop any laravel applications.
Step 1: Create Interface
Now in first step we must need to create an Interface, before create a Repositories(app/Repositories) directory in app folder. Also we need to create User(app/Repositories/User) folder inside the Repositories folder.
Ok, nowat first we will create a UserInterface in User directory, so at first need to create UserInterface.php file and then need to put bellow code in that file:
app/Repositories/User/UserInterface.php
namespace App\Repositories\User;
interface UserInterface {
public function getAll();
public function find($id);
public function delete($id);
}
Step 2: Create Repository
Ok, now in this step we will create a UserRepository.php file for write the database login, in this file we will write our all database login code. so, at first need to create UserRepository.php file in User directory and then put the bellow code.
app/Repositories/User/UserRepository.php
namespace App\Repositories\User;
use App\Repositories\User\UserInterface as UserInterface;
use App\User;
class UserRepository implements UserInterface
{
public $user;
function __construct(User $user) {
$this->user = $user;
}
public function getAll()
{
return $this->user->getAll();
}
public function find($id)
{
return $this->user->findUser($id);
}
public function delete($id)
{
return $this->user->deleteUser($id);
}
}
Step 3: Create Service Provider
Now in this step we have to create a Service Provider for bind UserInterface and UserRepository class. So let’s create a file as UserRepoServiceProvide.php file in User folder and then need to put the following code:
namespace App\Repositories\User;
use Illuminate\Support\ServiceProvider;
class UserRepoServiceProvide extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('App\Repositories\User\UserInterface', 'App\Repositories\User\UserRepository');
}
}
Now, we need to add a server provide in app.php file, so then add UserRepoServiceProvide in our config/app.php and add the bellow line .
config/app.php
return [
....
'providers' => [
......,
App\Repositories\User\UserRepoServiceProvide::class,
]
....
]
Step 4: Create User Model
Okay now we have already User Model. i think, so we have to just need to add getAll(), findUser() and deleteUser() function. But we can also past bellow code too.
So, let’s start and put the bellow code on our model.
app/Models/User.php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function getAll()
{
return static::all();
}
public function findUser($id)
{
return static::find($id);
}
public function deleteUser($id)
{
return static::find($id)->delete();
}
}
Step 5: Use In Controller
Hope we are ready for use Our Repository Pattern in our Controller. So, let’s add the bellow code in your UserController.php file.
app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\User\UserInterface as UserInterface;
class UserController extends Controller
{
public function __construct(UserInterface $user)
{
$this->user = $user;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = $this->user->getAll();
return view('users.index',['users']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = $this->user->find($id);
return view('users.show',['user']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function delete($id)
{
$this->user->delete($id);
return redirect()->route('users');
}
}
Now at last just need to fire bellow command because we have to auto load class.
composer update php artisan optimize
Read Also : How to convert Object into Array in PHP?
Thanks for read. I hope it help you. For more you can follow us on facebook.