SlideShare a Scribd company logo
Repository Design
Pattern in Laravel
Samir Poudel
The elements of this language are entities called patterns. Each pattern describes
a problem that occurs over and over again in our environment, and then
describes the core of the solution to that problem, in such a way that you can use
this solution a million times over, without ever doing it the same way twice.
— Christopher Alexander
Design Patterns
1. Builder
2. Chain of responsibility
3. Command
4. Facade
5. Factory
6. Iterator
7. Mediator
8. Observer
9. Presenter
10.Repository
11.Singleton
12.Strategy
LARAVEL is always awesome to
use. So why to use these design
patterns ?
Repository Design Pattern
class CommonController extends
BaseController
{
public function all()
{
$contacts = Contacts:all();
return
View::make(‘contacts.all’,
compact($contacts));
}
}
So it’s the way right ?
Common Controller
1. Tied to eloquent.
2. Not easily testable.
3. Maintenance is tough.
4. Bugs.
5. bla bla bla bla
Now what ??
Repositories
1. Repository design pattern is actually simple. Its just the addition of another
layer into your system. The Service Layer.
2. By this you add another layer to communicate with your database and
other thing instead of controller.
3. No need to write multiple codes.
4. Easily changeable DB.
5. Less headache.
What’s a repository
class CommonRepository
{
public function all()
{
return Contacts::all();
}
}
Repository Controller
class CommonController extends BaseController
{
public function all()
{
$contactsRepo = new ContactRepository();
$contacts = $contactsRepo->all();
return View::make(‘contacts.list’, compact($contacts));
}
}
Project Structure
- app
-- lib
--- Repository
---- Storage
----- Common
Interface Class : CommonRepository.php
Eloquent Class Implemented from interface : EloquentCommonRepository.php
Creating the service provider to bind two repositories together
<?php namespace RepositoryStorage;
use IlluminateSupportServiceProvider;
class StorageServiceProvider extends ServiceProvider
{
public function register() {
$this->app->bind( RepositoryStorageUserCommonRepository,
RepositoryStorageCommonEloquentCommonRepository );
}
}
Finally in the providers listing you need to add it
'providers' => array( // -- RepositoryStorageStorageServiceProvider' ),
Implementation
<?php
use RepositoryStorageCommonCommonRepository as Common;
class CommonController extends BaseController {
public function __construct(Common $common)
{
$this->common = $common;
}
public function index()
{
return $this->common->function();
}
}
What if we change DB ?
Switching to any new database or any changes are simple now.
$this->app->bind( ‘RepositoryStorageCommonCommonRepository',
‘RepositoryStorageCommonMongoCommonRepository' );
I don’t have more to say ;)
Ways to connect with me
@samirpdl
www.samirpdl.com.np
samir@samirpdl.com.np

More Related Content

PPTX
Repository design pattern in laravel
PPTX
Building Large Scale PHP Web Applications with Laravel 4
PPTX
Laravel Webcon 2015
PPTX
Laravel ppt
PDF
What's New In Laravel 5
PPTX
Servlets - filter, listeners, wrapper, internationalization
PDF
Servlet Filter
PDF
Getting to know Laravel 5
Repository design pattern in laravel
Building Large Scale PHP Web Applications with Laravel 4
Laravel Webcon 2015
Laravel ppt
What's New In Laravel 5
Servlets - filter, listeners, wrapper, internationalization
Servlet Filter
Getting to know Laravel 5

What's hot (20)

PPTX
Laravel overview
PPTX
Java 8 New features
PDF
Reflection in Pharo: Beyond Smalltak
PDF
Why Laravel?
PDF
Object Oriented Programming with Laravel - Session 3
PPTX
Next stop: Spring 4
PPT
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
PPTX
A introduction to Laravel framework
PDF
From CakePHP to Laravel
PDF
Laravel level 2 (Let's Practical)
PPTX
Real World Asp.Net WebApi Applications
PDF
Laravel Level 1 (The Basic)
PDF
Servlet
PDF
Java Server Pages
PDF
Variables in Pharo5
PPTX
How to get full power from WebApi
PDF
Laravel level 0 (introduction)
PPTX
Introduction to Laravel Framework (5.2)
PPTX
Spring MVC 5 & Hibernate 5 Integration
PDF
All Aboard for Laravel 5.1
Laravel overview
Java 8 New features
Reflection in Pharo: Beyond Smalltak
Why Laravel?
Object Oriented Programming with Laravel - Session 3
Next stop: Spring 4
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
A introduction to Laravel framework
From CakePHP to Laravel
Laravel level 2 (Let's Practical)
Real World Asp.Net WebApi Applications
Laravel Level 1 (The Basic)
Servlet
Java Server Pages
Variables in Pharo5
How to get full power from WebApi
Laravel level 0 (introduction)
Introduction to Laravel Framework (5.2)
Spring MVC 5 & Hibernate 5 Integration
All Aboard for Laravel 5.1
Ad

Viewers also liked (20)

PPTX
Introduction the Repository Pattern
PDF
Repository and Unit Of Work Design Patterns
PDF
Laravel 5 In Depth
PDF
IoC&Laravel
PDF
Testing Rich Domain Models
PPTX
A Tour of EF Core's (1.1) Most Interesting & Important Features
PPTX
EF6 or EF Core? How Do I Choose?
PPTX
ASP.NET MVC 4 Request Pipeline Internals
PPTX
Dependency injection in asp.net core
PPTX
Generic Repository Pattern with ASP.NET MVC and EF
PPTX
ASP.NET MVC 4 - Routing Internals
PPTX
C# 6.0 - DotNetNotts
PDF
Day02 01 Advance Feature in C# DH TDT
PDF
Generic repository pattern with ASP.NET MVC and Entity Framework
PDF
WebApp #3 : API
PPT
Design Pattern For C# Part 1
PPTX
.Net Core
PDF
Introduction repository, ddd and unit test
PPTX
ASP.NET Core 1.0 Overview
PDF
Software Architecture: Styles
Introduction the Repository Pattern
Repository and Unit Of Work Design Patterns
Laravel 5 In Depth
IoC&Laravel
Testing Rich Domain Models
A Tour of EF Core's (1.1) Most Interesting & Important Features
EF6 or EF Core? How Do I Choose?
ASP.NET MVC 4 Request Pipeline Internals
Dependency injection in asp.net core
Generic Repository Pattern with ASP.NET MVC and EF
ASP.NET MVC 4 - Routing Internals
C# 6.0 - DotNetNotts
Day02 01 Advance Feature in C# DH TDT
Generic repository pattern with ASP.NET MVC and Entity Framework
WebApp #3 : API
Design Pattern For C# Part 1
.Net Core
Introduction repository, ddd and unit test
ASP.NET Core 1.0 Overview
Software Architecture: Styles
Ad

Similar to Repository design pattern in laravel - Samir Poudel (20)

ODP
Best practices tekx
PDF
Java 8 Overview
PDF
Make your application expressive
PDF
Osiąganie mądrej architektury z Symfony2
PDF
Rupicon 2014 Action pack
PPT
Features java9
PDF
Ruby On Rails
PDF
C# c# for beginners crash course master c# programming fast and easy today
PDF
Advanced Interfaces and Repositories in Laravel
ODP
Content Staging in Drupal 8
PDF
What is DDD and how could it help you
PPTX
Intro to Rails 4
KEY
Código Saudável => Programador Feliz - Rs on Rails 2010
PDF
React Native +Redux + ES6 (Updated)
ODP
Modular programming Using Object in Scala
DOCX
Repository Pattern in MVC3 Application with Entity Framework
PDF
Beyond MVC: from Model to Domain
PDF
Lambdas & Streams
PDF
Nt1310 Unit 3 Language Analysis
PDF
Intro to drupal_7_architecture
Best practices tekx
Java 8 Overview
Make your application expressive
Osiąganie mądrej architektury z Symfony2
Rupicon 2014 Action pack
Features java9
Ruby On Rails
C# c# for beginners crash course master c# programming fast and easy today
Advanced Interfaces and Repositories in Laravel
Content Staging in Drupal 8
What is DDD and how could it help you
Intro to Rails 4
Código Saudável => Programador Feliz - Rs on Rails 2010
React Native +Redux + ES6 (Updated)
Modular programming Using Object in Scala
Repository Pattern in MVC3 Application with Entity Framework
Beyond MVC: from Model to Domain
Lambdas & Streams
Nt1310 Unit 3 Language Analysis
Intro to drupal_7_architecture

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
top salesforce developer skills in 2025.pdf
PDF
AI in Product Development-omnex systems
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Digital Strategies for Manufacturing Companies
PPTX
ai tools demonstartion for schools and inter college
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Introduction to Artificial Intelligence
PDF
System and Network Administration Chapter 2
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Nekopoi APK 2025 free lastest update
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
top salesforce developer skills in 2025.pdf
AI in Product Development-omnex systems
Understanding Forklifts - TECH EHS Solution
Operating system designcfffgfgggggggvggggggggg
Softaken Excel to vCard Converter Software.pdf
CHAPTER 2 - PM Management and IT Context
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Digital Strategies for Manufacturing Companies
ai tools demonstartion for schools and inter college
How Creative Agencies Leverage Project Management Software.pdf
Reimagine Home Health with the Power of Agentic AI​
Introduction to Artificial Intelligence
System and Network Administration Chapter 2
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
How to Choose the Right IT Partner for Your Business in Malaysia
Nekopoi APK 2025 free lastest update
Which alternative to Crystal Reports is best for small or large businesses.pdf
L1 - Introduction to python Backend.pptx
Odoo Companies in India – Driving Business Transformation.pdf

Repository design pattern in laravel - Samir Poudel

  • 1. Repository Design Pattern in Laravel Samir Poudel
  • 2. The elements of this language are entities called patterns. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. — Christopher Alexander
  • 3. Design Patterns 1. Builder 2. Chain of responsibility 3. Command 4. Facade 5. Factory 6. Iterator 7. Mediator 8. Observer 9. Presenter 10.Repository 11.Singleton 12.Strategy
  • 4. LARAVEL is always awesome to use. So why to use these design patterns ?
  • 6. class CommonController extends BaseController { public function all() { $contacts = Contacts:all(); return View::make(‘contacts.all’, compact($contacts)); } } So it’s the way right ? Common Controller 1. Tied to eloquent. 2. Not easily testable. 3. Maintenance is tough. 4. Bugs. 5. bla bla bla bla
  • 8. Repositories 1. Repository design pattern is actually simple. Its just the addition of another layer into your system. The Service Layer. 2. By this you add another layer to communicate with your database and other thing instead of controller. 3. No need to write multiple codes. 4. Easily changeable DB. 5. Less headache.
  • 9. What’s a repository class CommonRepository { public function all() { return Contacts::all(); } }
  • 10. Repository Controller class CommonController extends BaseController { public function all() { $contactsRepo = new ContactRepository(); $contacts = $contactsRepo->all(); return View::make(‘contacts.list’, compact($contacts)); } }
  • 11. Project Structure - app -- lib --- Repository ---- Storage ----- Common Interface Class : CommonRepository.php Eloquent Class Implemented from interface : EloquentCommonRepository.php
  • 12. Creating the service provider to bind two repositories together <?php namespace RepositoryStorage; use IlluminateSupportServiceProvider; class StorageServiceProvider extends ServiceProvider { public function register() { $this->app->bind( RepositoryStorageUserCommonRepository, RepositoryStorageCommonEloquentCommonRepository ); } } Finally in the providers listing you need to add it 'providers' => array( // -- RepositoryStorageStorageServiceProvider' ),
  • 13. Implementation <?php use RepositoryStorageCommonCommonRepository as Common; class CommonController extends BaseController { public function __construct(Common $common) { $this->common = $common; } public function index() { return $this->common->function(); } }
  • 14. What if we change DB ? Switching to any new database or any changes are simple now. $this->app->bind( ‘RepositoryStorageCommonCommonRepository', ‘RepositoryStorageCommonMongoCommonRepository' );
  • 15. I don’t have more to say ;) Ways to connect with me @samirpdl www.samirpdl.com.np samir@samirpdl.com.np