Created
December 3, 2015 11:43
-
-
Save mateusgf/9b9797f4bff662a3e958 to your computer and use it in GitHub Desktop.
An repository example for: https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Film { | |
protected $queryCriteria; | |
public function where($key, $value) | |
{ | |
$this->queryCriteria .= "Findind the Film by $key:$value\n"; | |
return $this; | |
} | |
public function all() | |
{ | |
echo $this->queryCriteria; | |
} | |
} | |
class FilmRepository { | |
protected $film; | |
public function __construct(Film $film) | |
{ | |
$this->film = $film; | |
} | |
public function criteria(array $criteria) | |
{ | |
//logic for finding by criteria here | |
$this->film = $this->film->where('title', 'The Lord Of The Rings')->where('length', '120'); | |
return $this; | |
} | |
public function all() | |
{ | |
echo $this->film->all(); | |
} | |
} | |
//Then you can use: | |
$films = new FilmRepository(new Film()); | |
$films->criteria([])->all(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment