当前位置:网站首页>laravel model 注意事项
laravel model 注意事项
2022-06-24 19:41:00 【王道长的编程之路】
一、时间显示
在模型内定义时间格式,否则model返回 "2020-08-13T03:36:53.000000Z"格式
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;
class Models extends Model{
protected function serializeDate(DateTimeInterface $date)
{
return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
}
//或在模型内使用attr方法
public function getCreateTimeAttribute($value){
return $value;
}
}
二、新增
使用 model1::firstOrCreate()时需在模型内定义fillable,如
class Model1 extends Models{
protected $connection = 'connection_name';//数据库连接
public $timestamps = false;//如果表不含created_at 和 updated_at字段,或只含一个,都需设 $timestamps=false。或通过 CREATED_AT 和 UPDATED_AT 常量设置自定义字段名:
const CREATED_AT = 'create_time';
const UPDATED_AT = null;
protected $dateFormat = 'U';//默认时间存储格式 Y-m-d H:i:s,或通过$dateFormat属性自定义时间戳格式,该属性值通过date() 函数解析
protected $fillable = ['name', 'user_id', 'status'];
protected $dates = ["creat_at"]
protected $casts = [ //定义字段属性
'is_directory' => 'boolean',
];
...
}
三、模型分页
在模型查询操作中对查询结果进行排序和分页:
$posts = Post::where('views', '>', 0)->orderBy('id', 'desc')->offset(10)->limit(5)->get();
//或使用分页器
$users = DB::table('users')->paginate(15);
四、预载入
???
五、user 模型授权
Laravel 的 User 模型提供两个用于授权动作:can 和 cant。can 方法接收要授权的动作和对应的模型作为参数。如下,判断用户是否被授权更新给定的 Post 模型:
if ($user->can('update', $post)) {
}
六、模型监听事件
| 方法 | 功能 |
|---|---|
| retrieved | 获取到模型实例后触发 |
| creating | 插入到数据库前触发 |
| created | 插入到数据库后触发 |
| updating | 更新到数据库前触发 |
| updated | 更新到数据库后触发 |
| saving | 保存到数据库前触发(插入/更新之前,无论插入还是更新都会触发) |
| saved | 保存到数据库后触发(插入/更新之后,无论插入还是更新都会触发) |
| deleting | 从数据库删除记录前触发 |
| deleted | 从数据库删除记录后触发 |
| restoring | 恢复软删除记录前触发 |
| restored | 恢复软删除记录后触发 |
class Model1 extends Models{
...
public static function boot(){
parent::boot();
static::creating(function (Area $area) {
info("Model [Area] creating...");
});
}
...
}
注:批量更新时不会触发相应事件,因为直接走查询构建器完成的,绕过了模型方法。
边栏推荐
- The difference between interceptor and filter
- ThreadLocal local thread
- Programmers become gods by digging holes in one year, carrying flags in five years and becoming gods in ten years
- LeetCode Algorithm 剑指 Offer 52. 两个链表的第一个公共节点
- Feign project construction
- Attackg: constructing technical knowledge graph from cyber thread intelligence reports
- [QT] QT event handling
- A big factory interview must ask: how to solve the problem of TCP reliable transmission? 8 pictures for you to learn in detail
- Common sense of resolution
- 加分利器 不负所托 | 知道创宇获攻防演练防守方感谢信!
猜你喜欢

vulnhub DC: 2

Panorama of enterprise power in China SSD industry

关于某手滑块的一些更新(6-18,js逆向)
![[untitled]](/img/ed/847e678e5a652da74d04722bbd99ff.jpg)
[untitled]

Annotation

Database transaction Transanction

开发规范~参数校验异常、异常返回提示切面

【Mongodb】READ_ME_TO_RECOVER_YOUR_DATA,数据库被恶意删除

Design and implementation of spark offline development framework

High level application of SQL statements in MySQL database (II)
随机推荐
Dynamic menu, auto align
Non single file component
Epics record reference 3 -- fields common to all records
It's hard to hear C language? Why don't you take a look at my article (7) input and output
Panorama of enterprise power in China SSD industry
Talk about GC mechanism often asked in interview
Leetcode algorithm The first common node of two linked lists
大厂面试必问:如何解决TCP可靠传输问题?8张图带你详细学习
【武汉大学】考研初试复试资料分享
vulnhub DC: 2
Research and investment strategy report on China's building steel structure anticorrosive coating industry (2022 Edition)
详细了解Redis的八种数据类型及应用场景分析
Solve the problem of non secure websites requesting localhost to report CORS after chrome94
Programmers become gods by digging holes in one year, carrying flags in five years and becoming gods in ten years
LeetCode Algorithm 剑指 Offer II 027. 回文链表
ThreadLocal local thread
Parental delegation mechanism
研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
Tech Talk 活动回顾|云原生 DevOps 的 Kubernetes 技巧
2022年高处安装、维护、拆除考试模拟100题及模拟考试