{"id":6630,"date":"2023-04-25T04:32:30","date_gmt":"2023-04-25T04:32:30","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=6630"},"modified":"2023-09-15T07:28:29","modified_gmt":"2023-09-15T07:28:29","slug":"repository-pattern-with-examples-in-php","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/","title":{"rendered":"Repository Pattern With Examples in PHP"},"content":{"rendered":"<p>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.<br \/>\nAs a result, switching to a different data source or implementing structural changes to the current data source is made simpler.<\/p>\n<p><em>Steps to implement the repository pattern in Laravel:<\/em><\/p>\n<ol>\n<li>\n<h2>Create Repository Interface:<\/h2>\n<p>To understand this concept, we will take an example of a blog application. First, create a \u201cRepositories\u201d folder inside the app folder. Then create an Interfaces folder inside the repoRepositoriessitory directory to store all the interfaces.<\/p>\n<p>Now we will create a BlogRepositoryInterface class, which will have three methods:<\/p>\n<ul>\n<li>The all() method will return all blogs.<\/li>\n<li>To fetch all blogs created by a selected user, we will call the getByUse() method within the application.<\/li>\n<li>The search() method returns filtered blogs by their title.<\/li>\n<\/ul>\n<p><code>&lt;?php<br \/>\nnamespace App\\Repositories\\Interfaces;<br \/>\nuse App\\User;<br \/>\ninterface BlogRepositoryInterface<br \/>\n{<br \/>\npublic function allBlogs();<br \/>\npublic function getAllByUser(User $user);<br \/>\npublic function search($name);<br \/>\n}<\/code><\/li>\n<li>\n<h3>Interface implementation with Eloquent:<\/h3>\n<p>Now create a BlogRepository class in the Repositories folder to implement the BlogRepositoryInterface.<br \/>\n<code>&lt;?php<br \/>\nnamespace App\\Repositories;<br \/>\nuse App\\Models\\Blog;<br \/>\nuse App\\User;<br \/>\nuse App\\Repositories\\Interfaces\\BlogRepositoryInterface;<br \/>\nclass BlogRepository implements BlogRepositoryInterface<br \/>\n{<br \/>\npublic function allBlogs()<br \/>\n{<br \/>\nreturn Blog::all();<br \/>\n}<br \/>\npublic function getAllByUser(User $user)<br \/>\n{<br \/>\nreturn Blog::where('user_id'. $user-&gt;id)-&gt;get();<br \/>\n}<br \/>\npublic function search($name)<br \/>\n{<br \/>\nreturn Blog::where('title', 'LIKE', '% ' . $name . '%')<br \/>\n-&gt;get();<br \/>\n}<br \/>\n}<\/code><\/li>\n<li>\n<h3>Register a new service provider to use the repository in the entire application:<\/h3>\n<p>Now bind the BlogRepositoryInterface with the BlogRepository inside the RepositoryServiceProvider class as below:<br \/>\n<code>&lt;?php<br \/>\nnamespace App\\Providers;<br \/>\nuse App\\Repositories\\BlogRepository;<br \/>\nuse App\\Repositories\\Interfaces\\BlogRepositoryInterface;<br \/>\nuse Illuminate\\Support\\ServiceProvider;<br \/>\nclass RepositoryServiceProvider extends ServiceProvider<br \/>\n{<br \/>\npublic function register()<br \/>\n{<br \/>\n$this-&gt;app-&gt;bind(<br \/>\nBlogRepositoryInterface::class,<br \/>\nBlogRepository::class<br \/>\n);<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/li>\n<li>\n<h3>After that, add that service provider to the Configuration file placed in \u201cconfig\/app.php\u201d:<\/h3>\n<p><code>&lt;?php<br \/>\n...<br \/>\n'providers' =&gt; [<br \/>\n\/*<br \/>\n* Laravel Framework Service Providers...<br \/>\n*\/<br \/>\nIlluminate\\Auth\\AuthServiceProvider::class,<br \/>\n...<br \/>\n\/*<br \/>\n* Application Service Providers...<br \/>\n*\/<br \/>\nApp\\Providers\\AppServiceProvider::class,<br \/>\n...<br \/>\nApp\\Providers\\RepositoryServiceProvider::class,<br \/>\n],<\/code><\/li>\n<li>\n<h3>At the end, we will call that created repository in our Controller file as below:<\/h3>\n<p><code>&lt;?php<br \/>\nnamespace App\\Http\\Controllers;<br \/>\nuse App\\Repositories\\Interfaces\\BlogRepositoryInterface;<br \/>\nuse App\\User;<br \/>\nclass BlogController extends Controller<br \/>\n{<br \/>\nprivate $blogRepository;<br \/>\npublic function __construct(BlogRepositoryInterface $blogRepository)<br \/>\n{<br \/>\n$this-&gt;blogRepository = $blogRepository;<br \/>\n}<br \/>\npublic function index()<br \/>\n{<br \/>\n$blogs = $this-&gt;blogRepository-&gt;allBlogs();<br \/>\nreturn view('blog')-&gt;withBlogs($blogs);<br \/>\n}<br \/>\npublic function getBlogsDetailsbyUser($id)<br \/>\n{<br \/>\n$user = User::find($id);<br \/>\n$blogs = $this-&gt;blogRepository-&gt;getAllByUser($user);<br \/>\nreturn view('blog')-&gt;withBlogs($blogs);<br \/>\n}<br \/>\npublic function searchBlogs(Request $request)<br \/>\n{<br \/>\n$name = $request-&gt;input('name');<br \/>\n$blogs = $this-&gt;blogRepository-&gt;search($name);<br \/>\nreturn view('blog')-&gt;withBlogs($blogs);<br \/>\n}<br \/>\n}<\/code><\/li>\n<\/ol>\n<p><small><em>jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The <a href=\"https:\/\/www.studysection.com\/jquery-3.x-advanced\">jQuery Certification exam<\/a> by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An architectural layer that manages communication between the application and data source is represented by a repository. The key benefit<\/p>\n","protected":false},"author":1,"featured_media":6631,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[200,617],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Repository pattern with examples in PHP - SS Blog<\/title>\n<meta name=\"description\" content=\"An architectural layer that manages communication between the application and data source is represented by a repository.\" \/>\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\/repository-pattern-with-examples-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Repository pattern with examples in PHP - SS Blog\" \/>\n<meta property=\"og:description\" content=\"An architectural layer that manages communication between the application and data source is represented by a repository.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/\" \/>\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=\"2023-04-25T04:32:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-15T07:28:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/04\/Repository-pattern1.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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Repository Pattern With Examples in PHP\",\"datePublished\":\"2023-04-25T04:32:30+00:00\",\"dateModified\":\"2023-09-15T07:28:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/\"},\"wordCount\":284,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"php\",\"Repository\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/\",\"url\":\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/\",\"name\":\"Repository pattern with examples in PHP - SS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2023-04-25T04:32:30+00:00\",\"dateModified\":\"2023-09-15T07:28:29+00:00\",\"description\":\"An architectural layer that manages communication between the application and data source is represented by a repository.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Repository Pattern With Examples in PHP\"}]},{\"@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":"Repository pattern with examples in PHP - SS Blog","description":"An architectural layer that manages communication between the application and data source is represented by a repository.","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\/repository-pattern-with-examples-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Repository pattern with examples in PHP - SS Blog","og_description":"An architectural layer that manages communication between the application and data source is represented by a repository.","og_url":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2023-04-25T04:32:30+00:00","article_modified_time":"2023-09-15T07:28:29+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/04\/Repository-pattern1.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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Repository Pattern With Examples in PHP","datePublished":"2023-04-25T04:32:30+00:00","dateModified":"2023-09-15T07:28:29+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/"},"wordCount":284,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["php","Repository"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/","url":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/","name":"Repository pattern with examples in PHP - SS Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2023-04-25T04:32:30+00:00","dateModified":"2023-09-15T07:28:29+00:00","description":"An architectural layer that manages communication between the application and data source is represented by a repository.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/repository-pattern-with-examples-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Repository Pattern With Examples in PHP"}]},{"@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":316,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6630"}],"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=6630"}],"version-history":[{"count":2,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6630\/revisions"}],"predecessor-version":[{"id":7024,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6630\/revisions\/7024"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/6631"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=6630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=6630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=6630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}