在 Laravel 5 中使用 Repository 模式實現業務邏輯和數據訪問的分離:http://laravelacademy.org/post/3063.htmlphp
Eloquent: 集合:https://d.laravel-china.org/docs/5.3/eloquent-collectionshtml
集合:https://d.laravel-china.org/docs/5.3/collectionslaravel
Laravel & Lumen之Eloquent ORM使用速查-基礎部分:https://segmentfault.com/a/1190000005792671git
Laravel & Lumen之Eloquent ORM使用速查-進階部分:https://segmentfault.com/a/1190000005792708github
Laravel & Lumen之Eloquent ORM使用速查-高級部分:https://segmentfault.com/a/1190000005792734數據庫
Lumen 進階之數據庫交互,Eloquent ORM,Facades,Collection:http://blog.gxxsite.com/lumen-advance-database-interaction/segmentfault
github連接:https://github.com/andersao/l5-repository數組
簡書這篇講得很透徹:https://www.jianshu.com/p/dcaaf801c294app
這篇也很不錯:http://oomusou.io/laravel/laravel-architecture/post
實例講解
先經過migrations建user_log表以後,
使用migrations:http://www.cnblogs.com/cxscode/p/8371789.html
運行下面語句
php artisan make:repository UserLog
此時會建立:
app/Models/UserLog.php //對應Model
app/Repositories/UserLogRepository.php //對應倉儲類接口
app/Repositories/UserLogRepositoryEloquent.php //對應倉儲類
app/Models/UserLog.php
class UserLog extends Model implements Transformable { use TransformableTrait; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ // 'id', 'user_id', 'status', 'type', // 'deleted_at', // 'created_at', // 'updated_at', ]; protected $table = 'user_log'; protected $primaryKey = 'id'; }
$fillable默認是空數組,須要補填一些增刪改查要操做的字段,$table(表名)和$primaryKey(主鍵)通常沒有,最好本身補全一下
app/Repositories/UserLogRepository.php
interface UserLogRepository extends RepositoryInterface { // }
通常也是一個空接口,能夠根據需求加入須要實現的接口
app/Repositories/UserLogRepositoryEloquent.php
class UserLogRepositoryEloquent extends BaseRepository implements AddressRepository { /** * Specify Model class name * * @return string */ public function model() { return Address::class; } /** * Boot up the repository, pushing criteria */ public function boot() { $this->pushCriteria(app(RequestCriteria::class)); } }
默認有一個model獲取方法和一個boot啓動方法,能夠把倉儲作爲控制器和Model的中間層,能夠實現一些方法,控制器調倉儲,倉儲調Model