Laravel: Repository Pattern in practice

Sdílet
Vložit
  • čas přidán 21. 07. 2024
  • Hi guys!
    In this video series, I'll be showcasing a simple way to integrate repository pattern into your Laravel application. We'll start with a super simple example - an app that stores blog posts.
    Chapters
    00:00 - Introduction & Simple Blog Post
    06:12 - Why would you use it
    07:24 - Implementation
    26:05 - Wrap up
    For more content visit saaslaravel.com
  • Věda a technologie

Komentáře • 23

  • @harshadevapriyankarabandar5456

    I think controller and service layer is enough for most of the laravel applications. Hence we are re creating all the method in the repository which is already available in the laravel eloquent model, it seems like a repetitive work without any additional benefit.
    We can just keep our controller clean and pass the business login to service class , then resolve the function my accessing database via eloquent models and then return the result to controller.
    in that way we can use the laravel already build eloquent powers easily and quickly.

  • @verard0
    @verard0 Před rokem +1

    really nice video! you covered a lot of info! congrats for your work!

  • @rahmaabdelaziz4095
    @rahmaabdelaziz4095 Před rokem +1

    great work

  • @Muhammed-nani964
    @Muhammed-nani964 Před rokem +4

    I think service (action) + DTO is better you may want to create a video on it

  • @mohammadjavadrajabloo2711
    @mohammadjavadrajabloo2711 Před měsícem

    Good Video!

  • @user-ou5gg7pv4m
    @user-ou5gg7pv4m Před 10 měsíci

    Hello we made injection on BaseRepository for Model but ı did same things laravel give an error like " Target [Illuminate\Database\Eloquent\Model] is not instantiable while building [App\Http\Controllers\BlogController, App\Repositories\BlogRepository]" when ı make like " $this->app->bind(BlogRepositoryInterface::class,BlogRepository::class);" in AppServiceProvider for solition ı changed this as "
    $this->app->bind(BlogRepositoryInterface::class,function (){
    return new BlogRepository(new Blog());
    });" is there any solution ? Thank you for feeds from now ...

  • @parumhr1268
    @parumhr1268 Před 9 měsíci +1

    what are the advantages to implementing the BlogRepositoryInterface as to just injecting the BlogRepository directly in the constructor??

    • @saaslaravel
      @saaslaravel  Před 9 měsíci +1

      You have the freedom to change the implementation of the repository i.e you can change persistence layer or move it to a 3rd party without changing how it interacts with the rest of the application

  • @user-fx1qh8ml5v
    @user-fx1qh8ml5v Před 5 měsíci

    Its better to use BlogPost::query()->toBase()->first() instead of BlogPost::first()->toArray(), because in second scenario, laravel convert array from DB to Eloquent, and convert eloquent to array. In first scenario you get raw data without converting

  • @GergelyCsermely
    @GergelyCsermely Před 11 měsíci

    Hi!
    I would suggest to use Route::resource

    • @saaslaravel
      @saaslaravel  Před 11 měsíci +1

      Never. I hate those! They decrease visibility for me

  • @alexz75515
    @alexz75515 Před rokem +2

    Because this line is very long you can tell that this is an enterprise application... 😆

  • @DailyTechShot
    @DailyTechShot Před 5 měsíci +3

    Please people, do not use a repository pattern in Laravel. Laravel has a beautiful active record solution and a repository pattern would only make sense when you would actually want to swap your database implementation, from let's say Mysql to Mongo for example. Do not use 'patterns' for the sake of using them. .

    • @hydrokat143
      @hydrokat143 Před 2 měsíci

      eloquent can switch over databases with some config changes in the database config and/or eloquent model.
      question is… is switching from sql to nosql or vv a thing?

  • @himasami2200
    @himasami2200 Před rokem

    can u please give me a source code of this video to study?

    • @saaslaravel
      @saaslaravel  Před rokem +1

      I don't have source code for it unfortunately. Will be doing that for any videos going forward 😄

  • @topvova
    @topvova Před rokem +2

    Why add an extra layer to Eloquent with interfaces and DTOs?
    This approach only clutters the code and reduces its maintainability.

    • @saaslaravel
      @saaslaravel  Před rokem +3

      I never use it personally but some of the reasons include decoupling the entity model from the database or organisation and reuse of more complex queries

    • @impulserr
      @impulserr Před 5 měsíci +2

      You can mock it easier for tests and it is cleaner imo. I like repositories for readonly eloquent/queries and services on top of that only if I need some more processing/logic + jobs if u need to update/insert/delete. Personally I think it is much better then throwing all to services.

    • @user-uh6qn1ef1d
      @user-uh6qn1ef1d Před 2 měsíci

      I personally do
      controller

  • @haingocduong5145
    @haingocduong5145 Před 4 měsíci

    Eloquent is a Repository . Don't use Repository Pattern if your Repository get data by Eloquent . it's meaningless

  • @user-fx1qh8ml5v
    @user-fx1qh8ml5v Před 5 měsíci

    Its better to use BlogPost::query()->toBase()->first() instead of BlogPost::first()->toArray(), because in second scenario, laravel convert array from DB to Eloquent, and convert eloquent to array. In first scenario you get raw data without converting