Create a database in mysql
1 2 3 4 5 |
drop table test; create table test( id int(11) AUTO_INCREMENT PRIMARY KEY, name text ); |
or create model by artisan command
1 |
php artisan make:model Test |
or create model in folder
1 |
php artisan make:model Models\\Test |
It will create Models folder and put Test in Models folder
Create a folder Repositories in app directory .
in repository
BaseInterface.php
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace App\Repositories; interface BaseInterface{ public function all(); // public function paginate($count); public function find($id,$relation=[]); /* public function findBy($field,$value); public function store($data); public function update($data); public function delete($id);*/ } |
BaseRepository.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
namespace App\Repositories; use App\Repositories\BaseInterface; abstract class BaseRepository implements BaseInterface { protected $modelName; public function all() { // TODO: Implement getAll() method. $instance=$this->getNewInstance(); return $instance->all(); } public function find($id,$relation=[]) { // TODO: Implement getById() method. $instance=$this->getNewInstance(); return $instance->with($relation)->find($id); } protected function getNewInstance(){ $model=$this->modelName; return new $model; } } |
Lets create a model call Test.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace App\Http\Models; use Illuminate\Database\Eloquent\Model; class Test extends Model { protected $table = 'test'; public $timestamps = false; // } |
Create a Test folder in Repositories and create ITestRepository.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php /** * Created by IntelliJ IDEA. * User: Akash * Date: 3/31/2017 * Time: 11:11 PM */ namespace App\Repositories\Test; interface ITestRepository extends BaseInterface{ // public function getAll(); } |
and TestRepository.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App\Repositories\Test; use App\Http\Models\Test; class TestRepository extends BaseRepository implements ITestRepository{ protected $modelName; public function __construct(Test $model){ $this->modelName=$model; } } |
in AppServiceProvider.php resolve the dependency
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Validator; use App\Repositories\Test\EloquentTest; use App\Repositories\Test\TestRepository; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { // Validator::extend('foo', function($attribute, $value, $parameters, $validator) { /*if(!empty($value) && (strlen($value) % 2) == 0){ return true; }*/ return $value=='foo'; }); } /** * Register any application services. * * @return void */ public function register() { // $this->app->singleton(ITestRepository::class,TestRepository::class); } } |
Now i can use it on controller like this.
Lets create a controller TestController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Repositories\Test\TestRepository; class TestController extends Controller { private $test; public function __construct(TestRepository $test){ $this->test=$test; } public function hello(){ return $this->test->findById(1); } // } |
and in route file add
1 |
Route::get('/', 'TestController@hello'); |
Basic Implementation
Complete implementation