{"id":3784,"date":"2021-01-20T03:41:03","date_gmt":"2021-01-20T03:41:03","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3784"},"modified":"2021-01-20T05:18:44","modified_gmt":"2021-01-20T05:18:44","slug":"why-do-we-need-a-repository-pattern","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/","title":{"rendered":"Why do we need a Repository pattern?"},"content":{"rendered":"<p>A repository provides separation between a domain and a persistent layer. A repository provides an interface to access data stored in the database or external resources. Data is returned in the form of objects.<\/p>\n<p>Repositories act as a bridge between the models and the controller. By using Repository Pattern we can decouple the hard dependencies of models from the controllers. The model should not be responsible for communicating with or extracting data from the database. Therefore, to keep your Laravel code clean and safe, we need to use repositories that will separate the responsibilities for which the model should never be responsible.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/img.png\" alt=\"repository pattern\"\/><\/p>\n<p>The use of Repository Pattern has many benefits, some of them are given below:<\/p>\n<ul>\n<li>Due to the centralization of the data access logic, it makes code easier to maintain.<\/li>\n<li>Business &#038; data access logic can be tested separately.<\/li>\n<li>Reduces duplication of code<\/li>\n<li>A lower chance for making programming errors<\/li>\n<\/ul>\n<p>Generally, we are writing code in this format.<br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/figure1.png\" alt=\"figure1\"\/><br \/>\nWhen we write code using this way, later on, change may turn out to be very difficult to implement, or even impossible. Every implementation should be based on interfaces. So later, if you want to perform changes you don\u2019t need to change the code in the whole application, but only create another <a href=\"https:\/\/studysection.com\/blog\/class-composition-in-python\/\">class<\/a> implementing the interface. This should be the code above written in the correct way.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/figure2.png\" alt=\"figure2\"\/><\/p>\n<p>In this example, if a client wants to change the data structure,  We will simply create a class that implements <strong>UserRepositoryInterface<\/strong> and contains all the changes and everything works again.<\/p>\n<h2>Let&#8217;s Start with code:<\/h2>\n<p> We should start by creating a Repository folder into our app folder and one more folder named Eloquent. So the folder structure should look like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/code2.png\" alt=\"repository pattern code2\"\/><\/p>\n<h3>EloquentRepositoryInterface.php<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/code3.png\" alt=\"repository pattern code3\"\/><\/p>\n<h3>BaseRepository.php:<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/code4.png\" alt=\"code4\"\/><\/p>\n<p>So that is our base repository. We will inject model classes into the constructor and store methods that may be used in every eloquent repository in this current example. It will be just <strong>created()<\/strong> and <strong>find()<\/strong> methods.<\/p>\n<h3>UserRepositoryInterface.php<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/code5.png\" alt=\"code5\"\/><br \/>\nThe next step is to implement our user repository using the parent class\/interface.<\/p>\n<h3>UserRepository.php<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/code6.png\" alt=\"code6\" \/><\/p>\n<p>And that\u2019s it! We have implemented a repository pattern in laravel.To let our application know which implementation of which interface we want to use we need to create a Laravel service provider. We will name it <strong>RepositoryServiceProvider<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/code7.png\" alt=\"code7\"\/><\/p>\n<p>After creating this file let\u2019s open it (App\/Providers\/RepositoryServiceProvider.php) and add some new line of codes:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/code8.png\" alt=\"code8\"\/><\/p>\n<p>Now register this service provider in our <strong>config\/app.php<\/strong>.<br \/>\nAdd below line to add <strong>RepositoryServiceProvider <\/strong>in our providers<br \/>\n<strong>App\\Providers\\RepositoryServiceProvider::class<\/strong>.<\/p>\n<p>Now our application knows what class it should use when we type an object by its interfaces. So we can simply use making logic like this in the controller.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/code9.png\" alt=\"code9\" \/><\/p>\n<p>This application will work the same way as before even if the data engine has changed and we didn\u2019t need to change even 1 line of code in our controller or anywhere else!<\/p>\n<p><small>If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/cakephp-developer-advanced-diploma\">PHP Certification Exams<\/a> are offered by StudySection along with other programming certification exams. <\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A repository provides separation between a domain and a persistent layer. A repository provides an interface to access data stored<\/p>\n","protected":false},"author":1,"featured_media":3785,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[149,617],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - Why do we need a Repository pattern?<\/title>\n<meta name=\"description\" content=\"A repository provides an interface to access data stored. Using Repository Pattern, we can decouple hard dependencies of models.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - Why do we need a Repository pattern?\" \/>\n<meta property=\"og:description\" content=\"A repository provides an interface to access data stored. Using Repository Pattern, we can decouple hard dependencies of models.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-20T03:41:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-20T05:18:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/pattern.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Why do we need a Repository pattern?\",\"datePublished\":\"2021-01-20T03:41:03+00:00\",\"dateModified\":\"2021-01-20T05:18:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/\"},\"wordCount\":543,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"database\",\"Repository\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/\",\"url\":\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/\",\"name\":\"StudySection Blog - Why do we need a Repository pattern?\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2021-01-20T03:41:03+00:00\",\"dateModified\":\"2021-01-20T05:18:44+00:00\",\"description\":\"A repository provides an interface to access data stored. Using Repository Pattern, we can decouple hard dependencies of models.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Why do we need a Repository pattern?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - Why do we need a Repository pattern?","description":"A repository provides an interface to access data stored. Using Repository Pattern, we can decouple hard dependencies of models.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - Why do we need a Repository pattern?","og_description":"A repository provides an interface to access data stored. Using Repository Pattern, we can decouple hard dependencies of models.","og_url":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2021-01-20T03:41:03+00:00","article_modified_time":"2021-01-20T05:18:44+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/01\/pattern.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Why do we need a Repository pattern?","datePublished":"2021-01-20T03:41:03+00:00","dateModified":"2021-01-20T05:18:44+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/"},"wordCount":543,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["database","Repository"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/","url":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/","name":"StudySection Blog - Why do we need a Repository pattern?","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2021-01-20T03:41:03+00:00","dateModified":"2021-01-20T05:18:44+00:00","description":"A repository provides an interface to access data stored. Using Repository Pattern, we can decouple hard dependencies of models.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/why-do-we-need-a-repository-pattern\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Why do we need a Repository pattern?"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":2298,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3784"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3784"}],"version-history":[{"count":4,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3784\/revisions"}],"predecessor-version":[{"id":3886,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3784\/revisions\/3886"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3785"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}