Base.php
2.8 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Base extends Model
{
protected $table = '';
public $allCount = 0;
//自动维护create_at创建时间 updated_at修改时间
public $timestamps = true;
//统一设置
protected $casts = [
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
];
/**
* 日期序列化
* @param \DateTimeInterface $date
* @return string
* @author zbj
* @date 2023/4/13
*/
protected function serializeDate(\DateTimeInterface $date): string
{
return $date->format('Y-m-d H:i:s');
}
/**
* @name 列表数据
* @return void
* @author :liyuhang
* @method
*/
public function lists($map, $p, $row, $order = 'id', $fields = ['*']){
//TODO::where(['id'=>'','name'=>''])
$lists = DB::table($this->table)->select($fields)->where($map)->orderBy($order)->paginate($row, ['*'], 'page', $p);
if (empty($lists)) {
return false;
}
$lists = $lists->toArray();
$this->allCount = $this->where($map)->count();
return $lists;
}
/**
* @param $map
* @param $order
* @param $fields
* @name :无分页列表
* @return mixed
* @author :liyuhang
* @method
*/
public function list($map,$order = 'id',$fields = ['*']){
$lists = $this->select($fields)->where($map)->orderBy($order)->get();
if (empty($lists)) {
return false;
}
$lists = $lists->toArray();
return $lists;
}
/**
* @param array:$condition
* @name :获取单条数据详情
* @return mixed
* @author :liyuhang
* @method get
*/
public function read($condition,$files = ['*'])
{
$info = $this->select($files)->where($condition)->first();
if (empty($info)) {
return false;
}
$info = $info->toArray();
return $info;
}
/**
* @name :新增
* @return void
* @author :liyuhang
* @method post
*/
public function add($data){
$data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = date('Y-m-d H:i:s');
return $this->insert($data);
}
/**
* @name :编辑
* @return void
* @author :liyuhang
* @method post
*/
public function edit($data,$condition){
$data['updated_at'] = date('Y-m-d H:i:s');
return $this->where($condition)->update($data);
}
/**
* @name : 删除数据
* @return void
* @author :liyuhang
* @method
*/
public function del($condition){
return $this->where($condition)->delete();
}
}