Laravel Eloquent 数据库关联查询

一对一 HasOne

tables

1
2
3
4
5
6
- users
- id
- name
- avatars
- user_id
- image

HasOne 关联

1
2
3
4
5
6
7
8
9
// App\Models\User
public function avatar(): HasOne
{
// $this->hasOne('class_name', 'foreign_key', 'local_key');
// foreign_key默认是表名去掉 s 加上 '_id' 后缀
// local_key 默认是 id
return $this->hasOne(Avatar::class);
// 即 $this->hasOne(Avatar::class, 'user_id', 'id');
}

HasOne 反向关联

1
2
3
4
5
6
7
// App\Models\Avatar
public function user(): BelongsTo
{
// $this->belongsTo(User::class, 'foreign_key', 'owner_key');
// return $this->belongsTo(User::class, 'user_id', 'id');
return $this->belongsTo(User::class); // 省略 'foreign_key', 'owner_key' 参数
}

数据操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 新建
$user = User::create(['name', '...']);
Avatar::create([
'user_id' => $user->id,
'image' => '...',
]);

// 更新
$user = User::find(1);
// 更新关联数据
$user->avatar()->update(['image' => '...']);
// 更新模型
$user->update(['name' => '...']);

// 反向关联操作 belongsTo 的 associate/dissociate
$avatar = Avatar::find(1);
$avatar->user()->dissociate(); // 把 user_id 设为 null
$avatar->save(); // 保存更改

$user = User::find(10);
$avatar->user()->associate($user); // 把 user_id 改为 10
$avatar->save();

一对多 HasMany

tables

1
2
3
4
5
6
7
- posts
- id
- title
- comments
- id
- post_id
- comment

模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// App\Models\Post 关联
public function comments(): HasMany
{
// return $this->hasMany('class_name', 'foreign_key', 'local_key');
return $this->hasMany(Comment::class, 'post_id', 'id');
return $this->hasMany(Comment::class); // 使用默认参数
}

// App\Models\Comment 反向关联
public function post(): BelongsTo
{
// $this->belongsTo(Post::class, 'foreign_key', 'owner_key');
return $this->belongsTo(Post::class, 'post_id', 'id');
return $this->belongsTo(Post::class); // 省略 'foreign_key', 'owner_key' 参数
}

//TODO

子查询

1
2
3
4
5
6
7
User::query()->whereIn(
'id',
UserRolePivot::query()
->select('user_id')
->where('role_id', $roleId)
)->get();
// SELECT * FROM user WHERE id IN( SELECT user_id FROM user_role_pivot WHERE role_id = $roleId)

分组查询

阅读更多

Composer 包下载加速

Composer的元数据存在国外的服务器,每次执行composer命令的时候,经常要下载很多的元数据,需要几分钟到十几分钟,效率极低。

我们可以设置使用国内composer镜像来加速。

1
2
3
4
# 查看 composer 项目配置
composer config -l
# 查看 composer 全局配置
composer config -gl

设置镜像命令:

1
2
3
4
5
# 阿里云镜像(不同步了,不推荐用)
# composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

# 腾讯云镜像(推荐用)
composer config -g repos.packagist composer https://mirrors.tencent.com/composer/

恢复默认源:

1
composer config -g --unset repos.packagist

从阿里云镜像换腾讯云镜像

最近安装 Laravel 10 总提示 匹配不到 doctrine/inflector 最小版本的错误:

1
- laravel/framework[v10.0.0, ..., v10.4.1] require doctrine/inflector ^2.0.5 -> found doctrine/inflector[2.0.x-dev, 2.1.x-dev] but it does not match your minimum-stability.

打开 packagist.org 看,明明最新包是 2.0.6,怎么会没有。

composer show doctrine/inflector -a 命令,看到的最新版本是 2.0.4 😭。可以实锤是镜像没同步,应用包太老了。

用命令 composer config -g --unset repos.packagist 恢复默认源,再执行 composer show doctrine/inflector -a,果然最新包时 2.0.6。

换腾讯云镜像,问题解决。

PhpStorm + Xdebug 远程调试PHP

这篇文章解决:

  • PhpStorm + xdebug 单步调试PHP的设置
  • 远程服务器PHP源码调试
  • 微信公众号接口/小程序接口服务的调试

有些应用需要部署到外网环境才能运行,我们使用 PhpStorm + Xdebug 在本地调试远程web服务器跑的代码。

安装xdebug扩展

1
pecl install xdebug

如果服务器部署的是php很新,安装xdebug不成功,可以试试 pecl install xdebug-beta

配置xdebug

在php.ini中添加如下配置

阅读更多