Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
213bdb5e
C
Coding Tree
项目概览
檀越@新空间
/
Coding Tree
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coding Tree
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
213bdb5e
编写于
7月 06, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
0cf9f727
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
203 addition
and
2 deletion
+203
-2
blog/laravel/index.md
blog/laravel/index.md
+1
-1
blog/laravel/model.md
blog/laravel/model.md
+201
-0
doc/php.md
doc/php.md
+1
-1
未找到文件。
blog/laravel/index.md
浏览文件 @
213bdb5e
...
...
@@ -32,5 +32,5 @@ dev:
[
构造器的增删改
](
blog/laravel/sql-builder-modify.md
)
[
模型的定义
](
blog/laravel/model.md
)
[
Model 模型
](
blog/laravel/model.md
)
blog/laravel/model.md
浏览文件 @
213bdb5e
...
...
@@ -146,4 +146,205 @@ User::destroy([2, 3, 4]);
// select * from `user` where `id` in (?, ?, ?)
```
## 批量赋值
设置 create 方法允许或不允许插入的数据字段
```
php
class
User
extends
Model
{
// 可批量赋值的属性
protected
$fillable
=
[
'name'
];
// 不可以批量赋值的属性
protected
$guarded
=
[
'name'
];
// 所有属性都可以批量赋值,$guarded 定义成一个空数组
protected
$guarded
=
[];
}
```
## 软删除
```
sql
CREATE
TABLE
`user`
(
`id`
int
NOT
NULL
AUTO_INCREMENT
,
`name`
varchar
(
50
)
NOT
NULL
,
`age`
int
NOT
NULL
DEFAULT
'0'
,
`create_time`
datetime
NOT
NULL
DEFAULT
CURRENT_TIMESTAMP
,
`update_time`
datetime
NOT
NULL
DEFAULT
CURRENT_TIMESTAMP
ON
UPDATE
CURRENT_TIMESTAMP
,
`delete_time`
datetime
DEFAULT
NULL
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
COMMENT
=
'用户表'
;
```
```
php
class
User
extends
Model
{
//开启软删除功能
use
SoftDeletes
;
// 软删除字段,默认deleted_at
const
DELETED_AT
=
'delete_time'
;
}
```
```
php
//删除一
$user
=
User
::
find
(
1
);
$user
->
delete
();
// select * from `user` where `user`.`id` = ? and `user`.`delete_time` is null limit 1
// update `user` set `delete_time` = ?, `user`.`update_time` = ? where `id` = ?
//删除二
User
::
destroy
(
2
);
// select * from `user` where `id` in (?) and `user`.`delete_time` is null
// update `user` set `delete_time` = ?, `user`.`update_time` = ? where `id` = ?
//软删除的数据不可见
User
::
get
();
// select * from `user` where `user`.`delete_time` is null
User
::
find
(
2
);
// select * from `user` where `user`.`id` = ? and `user`.`delete_time` is null limit 1
// 获取包含软删除的数据
User
::
withTrashed
()
->
get
();
// select * from `user`
// 获取某个被软删除的数据(即使不是软删除的也可以搜索到)
User
::
withTrashed
()
->
find
(
82
);
// select * from `user` where `user`.`id` = ? limit 1
// 获取所有软删除的数据
User
::
onlyTrashed
()
->
get
();
// select * from `user` where `user`.`delete_time` is not null
// 获取某个被软删除的数据(只有软删除的数据才可以被搜索到)
User
::
onlyTrashed
()
->
find
(
82
);
// select * from `user` where `user`.`delete_time` is not null and `user`.`id` = ? limit 1
// 判断是否是被软删除的数据
$user
=
User
::
withTrashed
()
->
find
(
1
);
$user
->
trashed
();
// 1
// select * from `user` where `user`.`id` = ? limit 1
// 将被软删除的数据回复正常
$user
=
User
::
onlyTrashed
()
->
find
(
2
);
$user
->
restore
();
// select * from `user` where `user`.`delete_time` is not null and `user`.`id` = ? limit 1
// update `user` set `delete_time` = ?, `user`.`update_time` = ? where `id` = ?
// [null,"2022-07-05 10:47:36",1]
// 开启软删除时的真实永久删除
$user
=
User
::
onlyTrashed
()
->
find
(
3
);
$user
->
forceDelete
();
// select * from `user` where `user`.`delete_time` is not null and `user`.`id` = ? limit 1
// delete from `user` where `id` = ?
```
## 模型的作用域
-
本地作用域
-
全局作用域
1、本地作用域
```
php
class
User
extends
Model
{
// 本地作用域 查询条件:成年人
public
function
scopeAdult
(
$query
)
{
return
$query
->
where
(
'age'
,
'>'
,
18
);
}
}
```
```
php
User
::
adult
()
->
get
();
// select * from `user` where `age` > ?
```
支持传递参数
```
php
class
User
extends
Model
{
// 本地作用域 支持传递参数
public
function
scopeQueryWhere
(
$query
,
$where
)
{
return
$query
->
where
(
$where
);
}
}
```
```
php
User
::
queryWhere
([
[
'age'
,
'>'
,
10
]
])
->
get
();
// select * from `user` where (`age` > ?)
```
2、全局作用域
定义全局作用域
```
php
<?php
namespace
App\Scopes
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Scope
;
class
StatusScope
implements
Scope
{
public
function
apply
(
Builder
$builder
,
Model
$model
)
{
return
$builder
->
where
(
'status'
,
1
);
}
}
```
```
php
class
User
extends
Model
{
// 启用全局作用域
protected
static
function
booted
()
{
parent
::
booted
();
// 全局公共模块
static
::
addGlobalScope
(
new
StatusScope
());
// 或者 只是针对某个模块
static
::
addGlobalScope
(
'status'
,
function
(
Builder
$builder
)
{
return
$builder
->
where
(
'status'
,
1
);
});
}
}
```
使用
```
php
User
::
queryWhere
([
[
'age'
,
'>'
,
10
]
])
->
get
();
// select * from `user` where (`age` > 10) and `status` = 1
```
取消全局条件
```
php
// 取消全局类的条件
User
::
withoutGlobalScope
(
StatusScope
::
class
)
->
get
();
// select * from `user`
// 取消名称为 status 的全局
User
::
withoutGlobalScope
(
'status'
)
->
get
();
// select * from `user`
```
https://www.bilibili.com/video/BV1gE411j78F?p=19&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
doc/php.md
浏览文件 @
213bdb5e
...
...
@@ -4,7 +4,7 @@
[
笔记:PHP 零基础入门到精通教程(P2 mysql 数据库 5 天)
](
blog/php-mysql/index.md
)
[
Laravel
](
/blog/laravel/index.md
)
[
Laravel
学习笔记
](
/blog/laravel/index.md
)
## 文章
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录