Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel Repositories

Laravel Repositories

Base understanding of Repositories in Laravel, how they are beneficial, and how to create an Abstract Repository for powerful functionality out of the box

Avatar for Robert Vrabel

Robert Vrabel

July 09, 2015
Tweet

Other Decks in Programming

Transcript

  1. @robertvrabel Web Developer @ Wayne State University since 2006 Dad

    Craft beer & Homebrewer since 2009 Tea Spotify/Music Junkie BBQ smoking (Pulled Pork & Ribs)
  2. What is a Repository? A class that sits between the

    controller and model. Responsible for retrieving/storing data (database, file system, S3, etc).
  3. My Progression Started by putting everything in the controllers and

    interacting directly with the models using eloquent.
  4. The need for a Repository As soon as I needed

    to reuse some code or update a complex eloquent call, I realized more abstraction needed to occur. This is where a repository came into play.
  5. Abstract Repository Since we noticed common functionality between our repositories

    we took it a step further and created an abstract class that all repositories inherit. This way they get tons of functionality right out of the box. Abstract Repository Methods: • function all(); • function paginate(); • function create(); • function update(); • function delete(); • function find(); • function findBy();
  6. Controller (Old) How would you reuse code from store method

    in the update method? You could create a method within the controller to do this, but then it is only accessible from the HTTP layer which isn’t very reusable.
  7. Repository (create) Create a new record using the model and

    then save additional data related to it.
  8. Repository (saveAdditional) Having a saveAdditional Method allows you to reuse

    it in the update method. - file upload - pivot table syncing
  9. Dependency Injection Construct your controllers with your repositories. This way

    you can reference $this->release repository anywhere!
  10. Links for you... Repository Pattern: https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/ Laravel Podcast: http://www.laravelpodcast.com/ Laravel

    Videos: http://laracasts.com Laravel Forums: http://laracasts.com/discuss Javscript Jabber Podcast: http://devchat.tv/js-jabber/ Ruby Rouges Podcast: http://devchat.tv/ruby-rogues/ Shop Talk Podcast: http://shoptalkshow.com/ Brewing Podcasts: http://www.thebrewingnetwork.com/shows (Sunday Session!)