Author - StudySection Post Views - 205 views
Repository pattern

Repository Pattern With Examples in PHP

An architectural layer that manages communication between the application and data source is represented by a repository. The key benefit of this commonly used pattern is that the application does not need to be aware of the data source being used or how it is being used.
As a result, switching to a different data source or implementing structural changes to the current data source is made simpler.

Steps to implement the repository pattern in Laravel:

  1. Create Repository Interface:

    To understand this concept, we will take an example of a blog application. First, create a “Repositories” folder inside the app folder. Then create an Interfaces folder inside the repoRepositoriessitory directory to store all the interfaces.

    Now we will create a BlogRepositoryInterface class, which will have three methods:

    • The all() method will return all blogs.
    • To fetch all blogs created by a selected user, we will call the getByUse() method within the application.
    • The search() method returns filtered blogs by their title.

    <?php
    namespace App\Repositories\Interfaces;
    use App\User;
    interface BlogRepositoryInterface
    {
    public function allBlogs();
    public function getAllByUser(User $user);
    public function search($name);
    }

  2. Interface implementation with Eloquent:

    Now create a BlogRepository class in the Repositories folder to implement the BlogRepositoryInterface.
    <?php
    namespace App\Repositories;
    use App\Models\Blog;
    use App\User;
    use App\Repositories\Interfaces\BlogRepositoryInterface;
    class BlogRepository implements BlogRepositoryInterface
    {
    public function allBlogs()
    {
    return Blog::all();
    }
    public function getAllByUser(User $user)
    {
    return Blog::where('user_id'. $user->id)->get();
    }
    public function search($name)
    {
    return Blog::where('title', 'LIKE', '% ' . $name . '%')
    ->get();
    }
    }

  3. Register a new service provider to use the repository in the entire application:

    Now bind the BlogRepositoryInterface with the BlogRepository inside the RepositoryServiceProvider class as below:
    <?php
    namespace App\Providers;
    use App\Repositories\BlogRepository;
    use App\Repositories\Interfaces\BlogRepositoryInterface;
    use Illuminate\Support\ServiceProvider;
    class RepositoryServiceProvider extends ServiceProvider
    {
    public function register()
    {
    $this->app->bind(
    BlogRepositoryInterface::class,
    BlogRepository::class
    );
    }
    }

  4. After that, add that service provider to the Configuration file placed in “config/app.php”:

    <?php
    ...
    'providers' => [
    /*
    * Laravel Framework Service Providers...
    */
    Illuminate\Auth\AuthServiceProvider::class,
    ...
    /*
    * Application Service Providers...
    */
    App\Providers\AppServiceProvider::class,
    ...
    App\Providers\RepositoryServiceProvider::class,
    ],

  5. At the end, we will call that created repository in our Controller file as below:

    <?php
    namespace App\Http\Controllers;
    use App\Repositories\Interfaces\BlogRepositoryInterface;
    use App\User;
    class BlogController extends Controller
    {
    private $blogRepository;
    public function __construct(BlogRepositoryInterface $blogRepository)
    {
    $this->blogRepository = $blogRepository;
    }
    public function index()
    {
    $blogs = $this->blogRepository->allBlogs();
    return view('blog')->withBlogs($blogs);
    }
    public function getBlogsDetailsbyUser($id)
    {
    $user = User::find($id);
    $blogs = $this->blogRepository->getAllByUser($user);
    return view('blog')->withBlogs($blogs);
    }
    public function searchBlogs(Request $request)
    {
    $name = $request->input('name');
    $blogs = $this->blogRepository->search($name);
    return view('blog')->withBlogs($blogs);
    }
    }

jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The jQuery Certification exam by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri