当前位置:网站首页>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...");
});
}
...
}
注:批量更新时不会触发相应事件,因为直接走查询构建器完成的,绕过了模型方法。
边栏推荐
- Based on the codeless platform, users deeply participated in the construction, and digital data + Nanjing Fiberglass Institute jointly built a national smart laboratory solution
- EPICS记录参考2--EPICS过程数据库概念
- 别再乱用了,这才是 @Validated 和 @Valid 的真正区别!!!
- [ROS play with turtle turtle]
- Problem solving - nested lists
- 大厂面试必问:如何解决TCP可靠传输问题?8张图带你详细学习
- Servlet
- OSPF basic content
- It's hard to hear C language? Why don't you take a look at my article (7) input and output
- 【Laravel系列7.9】测试
猜你喜欢

docker安装mysql-简单无坑

2022年高压电工考试模拟100题及在线模拟考试

Are you afraid of being asked MySQL related questions during the interview? This 30000 word essence summary + 100 interview questions, and it's enough to hang the interviewer

详细了解Redis的八种数据类型及应用场景分析

Dynamic menu, auto align

The usage difference between isempty and isblank is so different that so many people can't answer it

ThreadLocal local thread

vulnhub DC: 2

Environment configuration | vs2017 configuring openmesh source code and environment

Recommended course: workplace writing training
随机推荐
How to integrate Huawei cloud function services in fluent
2022-06-10 work record --js- obtain the date n days after a certain date
动态菜单,自动对齐
ThreadLocal local thread
JWT(Json Web Token)
cat写多行内容到文件
糖豆人登录报错解决方案
Annotation
Recommended movies: Northeast tiger, licorice pizza
C language operators and expressions
Research and investment strategy report on China's building steel structure anticorrosive coating industry (2022 Edition)
The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
Solve the problem of non secure websites requesting localhost to report CORS after chrome94
See how sparksql supports enterprise level data warehouse
[WSL] SSH Remote Connection and host port forwarding configuration
Database transaction Transanction
Design and implementation of spark offline development framework
Research Report on solar battery charger industry - market status analysis and development prospect forecast
vulnhub Vegeta: 1
Are you afraid of being asked MySQL related questions during the interview? This 30000 word essence summary + 100 interview questions, and it's enough to hang the interviewer