当前位置:网站首页>Notes for laravel model
Notes for laravel model
2022-06-24 23:12:00 【Wang Daochang's way of programming】
One 、 Time to show
Define the time format in the model , otherwise model return "2020-08-13T03:36:53.000000Z" Format
<?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');
}
// Or used within the model attr Method
public function getCreateTimeAttribute($value){
return $value;
}
}
Two 、 newly added
Use model1::firstOrCreate() It is necessary to define in the model fillable, Such as
class Model1 extends Models{
protected $connection = 'connection_name';// Database connection
public $timestamps = false;// If the table does not contain created_at and updated_at Field , Or just one , Must be set $timestamps=false. Or through CREATED_AT and UPDATED_AT Constant setting custom field name :
const CREATED_AT = 'create_time';
const UPDATED_AT = null;
protected $dateFormat = 'U';// Default time storage format Y-m-d H:i:s, Or through $dateFormat Property to customize the timestamp format , The value of this property is determined by date() Function analysis
protected $fillable = ['name', 'user_id', 'status'];
protected $dates = ["creat_at"]
protected $casts = [ // Define field properties
'is_directory' => 'boolean',
];
...
}
3、 ... and 、 Model Pagination
Sort and page the query results in the model query operation :
$posts = Post::where('views', '>', 0)->orderBy('id', 'desc')->offset(10)->limit(5)->get();
// Or use a pager
$users = DB::table('users')->paginate(15);
Four 、 preloading
???
5、 ... and 、user Model authorization
Laravel Of User The model provides two for authorizing actions :can and cant.can Method receives the action to be authorized and the corresponding model as parameters . as follows , Determine whether the user is authorized to update a given Post Model :
if ($user->can('update', $post)) {
}
6、 ... and 、 Model listening events
| Method | function |
|---|---|
| retrieved | Trigger after getting the model instance |
| creating | Triggered before inserting into the database |
| created | Triggered after inserting into the database |
| updating | Triggered before updating to the database |
| updated | Triggered after updating to the database |
| saving | Triggered before saving to the database ( Insert / Before updating , Both insert and update will trigger ) |
| saved | Triggered after saving to the database ( Insert / After the update , Both insert and update will trigger ) |
| deleting | Triggered before deleting a record from the database |
| deleted | Triggered after deleting a record from the database |
| restoring | Triggered before restoring soft delete records |
| restored | Triggered after recovering soft delete records |
class Model1 extends Models{
...
public static function boot(){
parent::boot();
static::creating(function (Area $area) {
info("Model [Area] creating...");
});
}
...
}
notes : The corresponding event will not be triggered during batch update , Because the query builder is used directly , Bypassing the model approach .
边栏推荐
- Spark 离线开发框架设计与实现
- Solve the problem of port occupation
- Vulnhub Vegeta: 1
- Blogs personal blog test point (manual test)
- Concurrent shared model management
- Dig deep into MySQL - resolve the difference between clustered and non clustered indexes
- Epics record reference 3 -- fields common to all records
- laravel 创建 service层
- Market trend report, technical innovation and market forecast of solar roof system in China
- Sword finger offer 42 Maximum sum of successive subarrays
猜你喜欢
![[ROS play with turtle turtle]](/img/94/4d1063f063d115aeef5cdf099278f8.png)
[ROS play with turtle turtle]

Cases of addition, deletion, modification and search of C # learning for two years and C # import and export (de duplication)

07_ Springboot for restful style

Second IPO of Huafang group: grown up in Zanthoxylum bungeanum, trapped in Zanthoxylum bungeanum

记录一下MySql update会锁定哪些范围的数据

案例解析:用「度量」提升企业研发效能|ONES Talk

Solve the problem of non secure websites requesting localhost to report CORS after chrome94

Dynamic menu, auto align

03_ Spingboot core profile
Paddledtx v1.0 has been released, and its security and flexibility have been comprehensively improved!
随机推荐
[text data mining] Chinese named entity recognition: HMM model +bilstm_ CRF model (pytoch) [research and experimental analysis]
2022 simulated 100 questions and simulated examination of high-altitude installation, maintenance and demolition
Recommended movies: Northeast tiger, licorice pizza
How to integrate Huawei cloud function services in fluent
2022年安全员-A证考题及答案
[WSL] SSH Remote Connection and host port forwarding configuration
推送Markdown格式信息到钉钉机器人
Selection (025) - what is the output of the following code?
Do you need to improve your code reading ability? It's a trick
Online group chat and dating platform test point
Research Report on market supply and demand and strategy of ceiling power supply device industry in China
研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
EPICS记录参考2--EPICS过程数据库概念
Écoutez le fichier markdown et mettez à jour Hot next. Page JS
How to add Google maps to a project
15 lines of code using mathematical formulas in wangeditor V5
[untitled]
Canvas to add watermark to pictures
2022 safety officer-a certificate examination questions and answers
【nvm】