Created
January 26, 2017 10:56
-
-
Save tbleckert/3800f4666439027d37f65c13bd35a13b to your computer and use it in GitHub Desktop.
Repository pattern with Eloquent
This file contains hidden or 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 | |
namespace App\Repositories\Post; | |
use App\Post; | |
class EloquentPostRepository implements PostRepositoryInterface | |
{ | |
protected $model; | |
public function __construct(Post $post) | |
{ | |
$this->model = $post; | |
} | |
public function getPaginatedIndex() | |
{ | |
return $this->model->orderBy('updated_at', 'desc')->paginate(20); | |
} | |
} |
This file contains hidden or 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 | |
namespace App\Http\Controllers\Blog; | |
use App\Repositories\Post\PostRepositoryInterface; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
class PostController extends Controller | |
{ | |
private $postRepository; | |
public function __construct(PostRepositoryInterface $postRepository) | |
{ | |
$this->postRepository = $postRepository; | |
} | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
$posts = $this->postRepository->getPaginatedIndex(); | |
return view('blog.index', ['posts' => $posts]); | |
} | |
} |
This file contains hidden or 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 | |
namespace App\Repositories\Post; | |
interface PostRepositoryInterface | |
{ | |
/** | |
* Get a sorted, paginated list of | |
* blog posts to use on the blog index | |
* | |
* @return \Illuminate\Support\Collection | |
*/ | |
public function getPaginatedIndex(); | |
} |
This file contains hidden or 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 | |
namespace App\Providers; | |
use App\Repositories\Post\EloquentPostRepository; | |
use App\Repositories\Post\PostRepositoryInterface; | |
use Illuminate\Support\ServiceProvider; | |
class PostServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Indicates if loading of the provider is deferred. | |
* | |
* @var bool | |
*/ | |
protected $defer = true; | |
/** | |
* Bootstrap the application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// | |
} | |
/** | |
* Register the application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->bind(PostRepositoryInterface::class, EloquentPostRepository::class); | |
} | |
/** | |
* Get the services provided by the provider. | |
* | |
* @return array | |
*/ | |
public function provides() | |
{ | |
return [PostRepositoryInterface::class]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment