Lumen Repository(仓储)

在 Laravel 5 中使用 Repository 模式实现业务逻辑和数据访问的分离:http://laravelacademy.org/post/3063.html

Eloquent: 集合:https://d.laravel-china.org/docs/5.3/eloquent-collections

集合:https://d.laravel-china.org/docs/5.3/collections

Laravel & Lumen之Eloquent ORM使用速查-基础部分:https://segmentfault.com/a/1190000005792671

Laravel & Lumen之Eloquent ORM使用速查-进阶部分:https://segmentfault.com/a/1190000005792708

Laravel & Lumen之Eloquent ORM使用速查-高级部分:https://segmentfault.com/a/1190000005792734

Lumen 进阶之数据库交互,Eloquent ORM,Facades,Collection:http://blog.gxxsite.com/lumen-advance-database-interaction/

 

github链接:https://github.com/andersao/l5-repository

简书这篇讲得很透彻:https://www.jianshu.com/p/dcaaf801c294

这篇也很不错:http://oomusou.io/laravel/laravel-architecture/

 

实例讲解

先通过migrations建user_log表之后,

使用migrations:http://www.cnblogs.com/cxscode/p/8371789.html

运行下面语句

1
php artisan make:repository UserLog

  

此时会创建:

app/Models/UserLog.php //对应Model

app/Repositories/UserLogRepository.php  //对应仓储类接口

app/Repositories/UserLogRepositoryEloquent.php //对应仓储类

 

app/Models/UserLog.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
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

1
2
3
4
interface UserLogRepository extends RepositoryInterface
{
    //
}

一般也是一个空接口,可以根据需求加入需要实现的接口

 

app/Repositories/UserLogRepositoryEloquent.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

 

posted @   程序生(Codey)  阅读(1420)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示