Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
989b15cd
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看板
提交
989b15cd
编写于
7月 15, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
e025ce6f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
368 addition
and
2 deletion
+368
-2
blog/laravel/debugbar.md
blog/laravel/debugbar.md
+3
-0
blog/laravel/index.md
blog/laravel/index.md
+7
-0
blog/laravel/model-relation-query.md
blog/laravel/model-relation-query.md
+0
-2
blog/laravel/model-relation-write.md
blog/laravel/model-relation-write.md
+358
-0
未找到文件。
blog/laravel/debugbar.md
浏览文件 @
989b15cd
...
...
@@ -7,10 +7,13 @@ composer8 require barryvdh/laravel-debugbar
```
生成一个配置文件
```
bash
php8 artisan vendor:publish
--provider
=
"Barryvdh
\D
ebugbar
\S
erviceProvider"
```
需要配合view模板使用
页面底部的调试工具
```
php
...
...
blog/laravel/index.md
浏览文件 @
989b15cd
...
...
@@ -42,4 +42,11 @@ dev:
[
模型的关联查询
](
/blog/laravel/model-relation-query.md
)
[
Debugbar 调试器
](
/blog/laravel/debugbar.md
)
[
模型的预加载
](
/blog/laravel/model-preload.md
)
[
模型的关联写入
](
/blog/laravel/model-relation-write.md
)
https://www.bilibili.com/video/BV1gE411j78F?p=32&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
blog/laravel/model-relation-query.md
浏览文件 @
989b15cd
...
...
@@ -157,5 +157,3 @@ select `user`.*,
(
select
count
(
*
)
from
`book`
where
`user`
.
`id`
=
`book`
.
`user_id`
and
`user_id`
=
?
)
as
`books_count`
from
`user`
```
https://www.bilibili.com/video/BV1gE411j78F?p=29&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
blog/laravel/model-relation-write.md
0 → 100644
浏览文件 @
989b15cd
# 模型的关联写入
模型
```
php
class
User
extends
Model
{
protected
$table
=
'user'
;
// 一对多关联 Book 表
public
function
books
()
{
return
$this
->
hasMany
(
Book
::
class
,
'user_id'
,
'id'
);
}
}
class
Book
extends
Model
{
protected
$table
=
'book'
;
// 取消批量赋值限制
protected
$guarded
=
[];
// 取消自动时间字段
// public $timestamps = false;
// 时间字段
public
const
CREATED_AT
=
'create_time'
;
public
const
UPDATED_AT
=
'update_time'
;
// 反向关联
public
function
user
()
{
return
$this
->
belongsTo
(
User
::
class
,
'user_id'
,
'id'
);
}
}
```
## 关联写入
```
php
//先限定用户
$user
=
User
::
find
(
1
);
//给这个用户关联的 book 新增一条记录
//user_id 会自动写入 19,title 自定义
$user
->
books
()
->
save
(
new
Book
([
'title'
=>
'《哈利波特》'
]));
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
insert
into
`book`
(
`title`
,
`user_id`
,
`update_time`
,
`create_time`
)
values
(
?
,
?
,
?
,
?
)
```
## 批量新增
```
php
$user
=
User
::
find
(
1
);
$user
->
books
()
->
saveMany
([
new
Book
([
'title'
=>
'《哈利波特》'
]),
new
Book
([
'title'
=>
'《指环王》'
])
]);
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [1]
insert
into
`book`
(
`title`
,
`user_id`
,
`update_time`
,
`create_time`
)
values
(
?
,
?
,
?
,
?
)
-- ["《哈利波特》",1,"2022-07-15 09:44:17","2022-07-15 09:44:17"]
insert
into
`book`
(
`title`
,
`user_id`
,
`update_time`
,
`create_time`
)
values
(
?
,
?
,
?
,
?
)
-- ["《指环王》",1,"2022-07-15 09:44:17","2022-07-15 09:44:17"]
```
## 插入数组新增
```
php
$user
=
User
::
find
(
1
);
$user
->
books
()
->
create
([
'title'
=>
'《哈利波特》'
]);
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- params: [1]
insert
into
`book`
(
`title`
,
`user_id`
,
`update_time`
,
`create_time`
)
values
(
?
,
?
,
?
,
?
)
-- params: ["《哈利波特》",1,"2022-07-15 09:48:14","2022-07-15 09:48:14"]
```
## 批量插入数组
```
php
$user
=
User
::
find
(
1
);
$user
->
books
()
->
createMany
([
[
'title'
=>
'《哈利波特》'
],
[
'title'
=>
'《指环王》'
]
]);
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [1]
insert
into
`book`
(
`title`
,
`user_id`
,
`update_time`
,
`create_time`
)
values
(
?
,
?
,
?
,
?
)
-- ["《哈利波特》",1,"2022-07-15 09:49:56","2022-07-15 09:49:56"]
insert
into
`book`
(
`title`
,
`user_id`
,
`update_time`
,
`create_time`
)
values
(
?
,
?
,
?
,
?
)
-- ["《指环王》",1,"2022-07-15 09:49:56","2022-07-15 09:49:56"]
```
PS:还有 findOrNew、firstOrNew、firstOrCreate 和 updateOrCreate 方法
> 区别:
-
new 没有保存到数据库,没有id
-
create 会保存到数据库,有id
## findOrNew
```
php
$user
=
User
::
find
(
1
);
return
$user
->
books
()
->
findOrNew
(
20
);
```
```
json
{
"user_id"
:
1
}
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [1]
select
*
from
`book`
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
and
`book`
.
`id`
=
?
limit
1
-- [1,20]
```
## firstOrNew
```
php
$user
=
User
::
find
(
1
);
return
$user
->
books
()
->
firstOrNew
([
'title'
=>
'《哈利波特》'
]);
```
```
json
{
"id"
:
3
,
"user_id"
:
1
,
"title"
:
"《哈利波特》"
,
"create_time"
:
"2022-07-15T01:43:21.000000Z"
,
"update_time"
:
"2022-07-15T01:43:21.000000Z"
,
"delete_time"
:
null
}
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [1]
select
*
from
`book`
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
and
(
`title`
=
?
)
limit
1
-- [1,"《哈利波特》"]
```
## firstOrCreate
```
php
$user
=
User
::
find
(
1
);
return
$user
->
books
()
->
firstOrCreate
([
'title'
=>
'《西游记》'
]);
```
```
json
{
"title"
:
"《西游记》"
,
"user_id"
:
1
,
"update_time"
:
"2022-07-15T01:56:00.000000Z"
,
"create_time"
:
"2022-07-15T01:56:00.000000Z"
,
"id"
:
9
}
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [1]
select
*
from
`book`
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
and
(
`title`
=
?
)
limit
1
-- [1,"《西游记》"]
insert
into
`book`
(
`title`
,
`user_id`
,
`update_time`
,
`create_time`
)
values
(
?
,
?
,
?
,
?
)
-- ["《西游记》",1,"2022-07-15 09:56:00","2022-07-15 09:56:00"]
```
## updateOrCreate
```
php
$user
=
User
::
find
(
1
);
return
$user
->
books
()
->
updateOrCreate
([
'title'
=>
'《水浒传》'
],
[
'delete_time'
=>
'2022-07-15 10:00:13'
]);
```
```
json
{
"id"
:
10
,
"user_id"
:
1
,
"title"
:
"《水浒传》"
,
"create_time"
:
"2022-07-15T02:00:13.000000Z"
,
"update_time"
:
"2022-07-15T02:05:02.000000Z"
,
"delete_time"
:
"2022-07-15 10:00:13"
}
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [1]
select
*
from
`book`
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
and
(
`title`
=
?
)
limit
1
-- [1,"《水浒传》"]
update
`book`
set
`delete_time`
=
?
,
`book`
.
`update_time`
=
?
where
`id`
=
?
-- ["2022-07-15 10:00:13","2022-07-15 10:05:02",10]
```
## 关联删除
```
php
// 删除 user_id=19 的书
$user
=
User
::
find
(
99
);
$user
->
books
()
->
delete
();
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [1]
delete
from
`book`
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
-- [1]
```
## 关联修改
```
php
// 修改 user_id=19 的书
$user
=
User
::
find
(
1
);
$user
->
books
()
->
update
([
'title'
=>
'《修改书籍》'
])
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [1]
update
`book`
set
`title`
=
?
,
`book`
.
`update_time`
=
?
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
-- ["《修改书籍》","2022-07-15 10:09:08",1]
```
## 修改关联的外键
```
php
// 修改掉书籍关联的用户,即:user_id 修改,换用户
$user
=
User
::
find
(
2
);
$book
=
Book
::
find
(
1
);
$book
->
user
()
->
associate
(
$user
);
$book
->
save
();
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
-- [2]
select
*
from
`book`
where
`book`
.
`id`
=
?
limit
1
-- [1]
update
`book`
set
`user_id`
=
?
,
`book`
.
`update_time`
=
?
where
`id`
=
?
-- [2,"2022-07-15 10:11:31",1]
```
```
php
// PS:如果想取消一本书的拥有者,
// 比如将 user_id 设置为 null,字段要设置可以 null;
$book
=
Book
::
find
(
1
);
$book
->
user
()
->
dissociate
();
$book
->
save
();
```
```
sql
select
*
from
`book`
where
`book`
.
`id`
=
?
limit
1
-- [1]
update
`book`
set
`user_id`
=
?
,
`book`
.
`update_time`
=
?
where
`id`
=
?
-- [null,"2022-07-15 10:13:34",1]
```
## 默认模型
搜索书籍的对应用户的时候,空null字段会导致用户出现null数据
采用空对象默认模型的方式,去解决这个问题
```
php
//Book.php
public
function
user
()
{
return
$this
->
belongsTo
(
User
::
class
,
'user_id'
,
'id'
)
->
withDefault
([
'id'
=>
0
,
'name'
=>
'游客用户'
]);
}
```
```
php
$book
=
Book
::
find
(
1
);
$book
->
load
([
'user'
]);
return
$book
;
```
```
json
{
"id"
:
1
,
"user_id"
:
null
,
"title"
:
"《三国演义》"
,
"create_time"
:
"2022-07-15T02:10:44.000000Z"
,
"update_time"
:
"2022-07-15T02:13:34.000000Z"
,
"delete_time"
:
null
,
"user"
:
{
"id"
:
0
,
"name"
:
"游客用户"
}
}
```
```
sql
select
*
from
`book`
where
`book`
.
`id`
=
?
limit
1
-- [1]
select
*
from
`user`
where
0
=
1
-- []
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录