当前位置:网站首页>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...");
});
}
...
}
注:批量更新时不会触发相应事件,因为直接走查询构建器完成的,绕过了模型方法。
边栏推荐
- 【Laravel系列7.9】测试
- Leetcode algorithm The first common node of two linked lists
- Research and investment strategy report on China's bridge anticorrosive coating industry (2022 Edition)
- The usage difference between isempty and isblank is so different that so many people can't answer it
- [laravel series 7.9] test
- 【nvm】
- EPICS记录参考3 -- 所有记录都有的字段
- Parental delegation mechanism
- Principle of IP routing
- Learn more about redis' eight data types and application scenario analysis
猜你喜欢

关于某手滑块的一些更新(6-18,js逆向)

京东618会议平板排行榜公布,新锐黑马品牌会参谋角逐前三名,向国货老大华为学习

shopee开店入驻流水如何提交?

canvas 实现图片新增水印
![[postgraduate entrance examination English] prepare for 2023, learn list8 words](/img/25/d1f2c2b4c0958d381db87e5ef96df9.jpg)
[postgraduate entrance examination English] prepare for 2023, learn list8 words

Simulated 100 questions and online simulated examination of high voltage electrician examination in 2022

Non single file component

Recommended course: workplace writing training

Panorama of enterprise power in China SSD industry

非单文件组件
随机推荐
Rip protocol of dynamic routing protocol
EPICS記錄參考3 -- 所有記錄都有的字段
Do you need to improve your code reading ability? It's a trick
Tech Talk 活动回顾|云原生 DevOps 的 Kubernetes 技巧
Introduction to machine learning compilation course learning notes lesson 1 overview of machine learning compilation
MySQL kills 10 people. How many questions can you hold on to?
shopee开店入驻流水如何提交?
Sword finger offer 13 Range of motion of robot
Sword finger offer 42 Maximum sum of successive subarrays
STP spanning tree protocol Foundation
Beijiafu (p+f) R2000 modified radar IP
Source code reading | the process of reading text format STL by openmesh
关于某手滑块的一些更新(6-18,js逆向)
Research and investment strategy report on China's building steel structure anticorrosive coating industry (2022 Edition)
2022年高压电工考试模拟100题及在线模拟考试
docker安装mysql-简单无坑
Web security XSS foundation 06
Solve the problem of port occupation
[WSL] SSH Remote Connection and host port forwarding configuration
Solution to the login error of tangdou people