Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
e26531a3
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看板
提交
e26531a3
编写于
7月 12, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
6821839b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
369 addition
and
4 deletion
+369
-4
blog/laravel/debugbar.md
blog/laravel/debugbar.md
+35
-0
blog/laravel/index.md
blog/laravel/index.md
+4
-2
blog/laravel/model-preload.md
blog/laravel/model-preload.md
+169
-0
blog/laravel/model-relation-query.md
blog/laravel/model-relation-query.md
+161
-0
blog/laravel/model-relation.md
blog/laravel/model-relation.md
+0
-2
未找到文件。
blog/laravel/debugbar.md
0 → 100644
浏览文件 @
e26531a3
# Debugbar 调试器
安装Debugbar
```
bash
composer8 require barryvdh/laravel-debugbar
```
生成一个配置文件
```
bash
php8 artisan vendor:publish
--provider
=
"Barryvdh
\D
ebugbar
\S
erviceProvider"
```
页面底部的调试工具
```
php
// @deprecated
// use Barryvdh\Debugbar\Facade as DebugBar;
use
Barryvdh\Debugbar\Facades\Debugbar
;
DebugBar
::
info
(
'信息!'
);
```
```
php
// config/debugbar.php
// 关闭调试工具
'enabled'
=>
env
(
'DEBUGBAR_ENABLED'
,
false
),
```
```
php
//手工开启或关闭
DebugBar
::
enable
();
DebugBar
::
disable
();
```
\ No newline at end of file
blog/laravel/index.md
浏览文件 @
e26531a3
...
...
@@ -36,7 +36,9 @@ dev:
[
集合 collection
](
/blog/laravel/collection.md
)
[
模型的数据集合
](
/blog/laravel/model-collection.md
)
[
模型的数据集合
](
/blog/laravel/model-collection.md
)
[
模型关联 relation
](
/blog/laravel/model-relation.md
)
[
模型关联 relation
](
/blog/laravel/model-relation.md
)
[
模型的关联查询
](
/blog/laravel/model-relation-query.md
)
blog/laravel/model-preload.md
0 → 100644
浏览文件 @
e26531a3
# 模型的预加载
预加载: 解决关联查询中产生的
`N+1`
次查询带来的资源消耗
模型
```
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'
;
// 反向关联
public
function
user
()
{
return
$this
->
belongsTo
(
User
::
class
,
'user_id'
,
'id'
);
}
}
```
要获取所有书籍的作者(或拥有者),普通查询方案如下
```
php
//获取所有书籍列表
$books
=
Book
::
all
();
//遍历每一本书
foreach
(
$books
as
$book
)
{
//每一本书的关联用户的姓名
DebugBar
::
info
(
$book
->
user
->
username
);
}
```
```
sql
-- N+1 条,就是起初获取全部数据的 1 条和,遍历的 N 条
select
*
from
`book`
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
```
使用with()关键字,进行预载入设置,提前将SQL整合
```
php
//with 关键字预载入
$books
=
Book
::
with
(
'user'
)
->
get
();
foreach
(
$books
as
$book
)
{
DebugBar
::
info
(
$book
->
user
->
username
);
}
```
```
sql
select
*
from
`book`
select
*
from
`user`
where
`user`
.
`id`
in
(
1
)
```
```
php
// 预载入设置指定的列
$books
=
Book
::
with
(
'user:id,name'
)
->
get
();
```
```
sql
select
*
from
`book`
select
`id`
,
`name`
from
`user`
where
`user`
.
`id`
in
(
1
)
```
模型中定义默认加载的关联
```
php
class
Book
extends
Model
{
protected
$table
=
'book'
;
// 默认加载的关联
protected
$with
=
[
'user'
];
// 反向关联
public
function
user
()
{
return
$this
->
belongsTo
(
User
::
class
,
'user_id'
,
'id'
);
}
}
```
预载入筛选
```
php
Book
::
with
([
'user'
=>
function
(
$query
)
{
$query
->
where
(
'id'
,
1
);
}
])
->
get
();
```
```
sql
select
*
from
`book`
select
*
from
`user`
where
`user`
.
`id`
in
(
1
)
and
`id`
=
?
```
延迟预载入
```
php
$books
=
Book
::
all
();
$books
->
load
(
'user'
);
```
```
sql
select
*
from
`book`
select
*
from
`user`
where
`user`
.
`id`
in
(
1
)
```
实现延迟关联统计
```
php
$users
=
User
::
all
();
$users
->
loadCount
(
'book'
);
```
```
sql
select
*
from
`user`
select
`id`
,
(
select
count
(
*
)
from
`book`
where
`user`
.
`id`
=
`book`
.
`user_id`
)
as
`books_count`
from
`user`
where
`user`
.
`id`
in
(
?
,
?
)
```
```
json
[
{
"id"
:
1
,
"name"
:
"曹真"
,
"age"
:
23
,
"create_time"
:
"2022-07-05 10:32:48"
,
"update_time"
:
"2022-07-05 10:47:36"
,
"delete_time"
:
null
,
"books_count"
:
2
},
{
"id"
:
2
,
"name"
:
"曹丕"
,
"age"
:
21
,
"create_time"
:
"2022-07-05 10:36:47"
,
"update_time"
:
"2022-07-05 10:47:22"
,
"delete_time"
:
null
,
"books_count"
:
0
}
]
```
\ No newline at end of file
blog/laravel/model-relation-query.md
0 → 100644
浏览文件 @
e26531a3
# 模型的关联查询
```
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'
;
// 反向关联
public
function
user
()
{
return
$this
->
belongsTo
(
User
::
class
,
'user_id'
,
'id'
);
}
}
```
查询
```
php
//下面两种查询是一样的;
$books
=
User
::
find
(
1
)
->
books
;
$books
=
User
::
find
(
19
)
->
book
()
->
get
();
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
select
*
from
`book`
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
```
```
php
//可以采用 where 筛选或闭包
$books
=
User
::
find
(
1
)
->
books
()
->
where
(
'id'
,
1
)
->
orWhere
(
'id'
,
11
)
->
get
();
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
select
*
from
`book`
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
and
`id`
=
?
or
`id`
=
?
```
```
php
$books
=
User
::
find
(
1
)
->
books
()
->
where
(
function
(
$query
)
{
$query
->
where
(
'id'
,
1
)
->
orWhere
(
'id'
,
11
);
})
->
get
();
```
```
sql
select
*
from
`user`
where
`user`
.
`id`
=
?
limit
1
select
*
from
`book`
where
`book`
.
`user_id`
=
?
and
`book`
.
`user_id`
is
not
null
and
(
`id`
=
?
or
`id`
=
?
)
```
has()方法,可以查询某些条件下的关联查询数据
```
php
// 获取存在关联书籍的用户列表(言下之意:至少一本书)
$users
=
User
::
has
(
'books'
)
->
get
();
```
```
sql
select
*
from
`user`
where
exists
(
select
*
from
`book`
where
`user`
.
`id`
=
`book`
.
`user_id`
)
```
```
php
// 获取存在关联书籍(并超过 3 条)的用户列表
$users
=
User
::
has
(
'book'
,
'>='
,
3
)
->
get
();
```
```
sql
select
*
from
`user`
where
(
select
count
(
*
)
from
`book`
where
`user`
.
`id`
=
`book`
.
`user_id`
)
>=
3
```
使用whereHas()方法,创建闭包查询
```
php
//whereHas 闭包用法
$users
=
User
::
whereHas
(
'books'
,
function
(
$query
)
{
//这里$query 是 book 表,通过 user_id 查询,返回 user 表数据
$query
->
where
(
'user_id'
,
19
);
})
->
get
();
```
```
sql
select
*
from
`user`
where
exists
(
select
*
from
`book`
where
`user`
.
`id`
=
`book`
.
`user_id`
and
`user_id`
=
?
)
```
使用doesntHave()方法,即has()的反向操作
```
php
// 获取不存在关联书籍的用户列表,闭包用法:whereDoesntHave()
$users
=
User
::
doesntHave
(
'books'
)
->
get
();
```
```
sql
select
*
from
`user`
where
not
exists
(
select
*
from
`book`
where
`user`
.
`id`
=
`book`
.
`user_id`
)
```
使用withCount()方法,可以进行关联统计
```
php
//关联统计,会自动给一个 book_count 字段 //统计每个用户有多少本书
User
::
withCount
(
'books'
)
->
get
();
```
```
sql
select
`user`
.
*
,
(
select
count
(
*
)
from
`book`
where
`user`
.
`id`
=
`book`
.
`user_id`
)
as
`books_count`
from
`user`
```
```
php
// 给多个关系添加统计:profile_count,book_count
User
::
withCount
([
'profile'
,
'books'
])
->
get
();
```
```
sql
select
`user`
.
*
,
(
select
count
(
*
)
from
`profile`
where
`user`
.
`id`
=
`profile`
.
`user_id`
)
as
`profile_count`
,
(
select
count
(
*
)
from
`book`
where
`user`
.
`id`
=
`book`
.
`user_id`
)
as
`books_count`
from
`user`
```
```
php
//关联统计再结合闭包进行筛选,还可以设置别名
User
::
withCount
([
'profile'
,
'books'
=>
function
(
$query
)
{
//这里限制被统计的记录
$query
->
where
(
'user_id'
,
19
);
}])
->
get
();
```
```
sql
select
`user`
.
*
,
(
select
count
(
*
)
from
`profile`
where
`user`
.
`id`
=
`profile`
.
`user_id`
)
as
`profile_count`
,
(
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.md
浏览文件 @
e26531a3
...
...
@@ -621,5 +621,3 @@ where `user_role`.`user_id` = ? and `user_role`.`id` = ?
-
多态一对多
-
多态多对多
https://www.bilibili.com/video/BV1gE411j78F?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录