Chapter 6 - Laravel Design Pattern
Hello Artisan's, welcome to the 6th chapter of being an Artisanary. Ok the first question is, why we use design pattern? Using design patterns in Laravel can make your code more efficient, easier to maintain, and more scalable, which can save you time and effort in the long run. So if you already complete the previous chapters/sections, then you're good to go, if not my recommendation would be please complete the previous chapters. Because we'll use the same old repository that we use from chapter 4.
Note: Tested on Laravel 10.0
There is many design patterns available like the Singleton pattern, the Factory pattern, the Observer pattern, the Adapter pattern, and the Decorator pattern, among others we use Repository Pattern for our whole series.
The Repository Pattern is a design pattern that provides a layer of abstraction between the application's business logic and the underlying data source, allowing for more modular, testable, and maintainable code.
How it works in Laravel?
To use in Laravel first we've to create a folder under app named Repositories, so our app directory will be look like below
Then we'll create a php file inside the Repositories directory. Suppose we'll create a Repository file called BlogRepository.php. After creating a file we'll call all our database operation in this repository file. Not on the controllers or blade files. So first create model by firing the below command.
php artisan make:model Blog -m
Then we'll define a method in our BlogRepository called all() where we'll fetch our all blogs with paginate(). See the below source code.
<?php
namespace App\Repositories;
use App\Models\Blog;
class BlogRepository
{
public function all()
{
return Blog::latest()->paginate();
}
}
And when we need to retrieve the list of Blogs, we need to just call the all() method form BlogRepository. And that's how we use Repository Pattern.
Now we'll open the BlogController which we'll create in last chapter and we'll use our repository class in that controller. So, look at the below source code,
<?php
namespace App\Http\Controllers;
use App\Http\Requests\BlogRequest;
use App\Repositories\BlogRepository;
use Illuminate\Http\Request;
class BlogController extends Controller
{
protected $blogRepository;
public function __construct(BlogRepository $blogRepository)
{
$this->blogRepository = $blogRepository;
}
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse
{
try {
$data = [
'blogs' => $this->blogRepository->all() //our repository pattern method
];
return view('blogs.index', $data);
} catch (\Exception $e) {
return redirect()->back()->with('error', $e->getMessage());
}
}
public function create()
{
//
}
public function store(BlogRequest $request)
{
//
}
public function show(string $id)
{
//
}
public function edit(string $id)
{
//
}
public function update(BlogRequest $request, string $id)
{
//
}
public function destroy(string $id)
{
//
}
}
So, look at the index() method, where we need to fetch all the blogs but instead of querying it we just the call the all() methods of BlogRepository. And thus we'll use in the controller and also it's saves our time when it come to the complex query.
So last of all, we'll see the usage of repository class in this chapter. And yes keep up to date with the Github repository. That's it for today. See you in my next chapter. Happy coding ๐.