<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Status extends Model
{
protected $fillable = ['content'];
public function user()
{
return $this->belongsTo(User::class);
}
}
@Joy_he 不是很理解 文档我也看过了, Status::whereIn('user_id', $user_ids) user_ids已经有了 然后查询 微博列表 为什么还去关联一下users 表 在我看来来 就是单表查询啊~~~
这里面的with绝对是多余的。我把非with的和有with的方式都打印出来了。
打印出的数组为
with后:
[0] => Array
(
[query] => select * from `users` where `id` = ? limit 1
[bindings] => Array
(
[0] => 105
)
[time] => 11.1
)
[1] => Array
(
[query] => select `users`.*, `followers`.`follower_id` as `pivot_follower_id`, `followers`.`user_id` as `pivot_user_id` from `users` inner join `followers` on `users`.`id` = `followers`.`user_id` where `followers`.`follower_id` = ?
[bindings] => Array
(
[0] => 105
)
[time] => 0.43
)
[2] => Array
(
[query] => select count(*) as aggregate from `statuses` where `user_id` in (?, ?, ?, ?)
[bindings] => Array
(
[0] => 101
[1] => 102
[2] => 3
[3] => 105
)
[time] => 0.49
)
[3] => Array
(
[query] => select * from `statuses` where `user_id` in (?, ?, ?, ?) order by `created_at` desc limit 30 offset 120
[bindings] => Array
(
[0] => 101
[1] => 102
[2] => 3
[3] => 105
)
[time] => 1.3
)
[4] => Array
(
[query] => select * from `users` where `users`.`id` in (?, ?, ?)
[bindings] => Array
(
[0] => 101
[1] => 102
[2] => 105
)
[time] => 0.32
)
非with:
[0] => Array
(
[query] => select * from `users` where `id` = ? limit 1
[bindings] => Array
(
[0] => 105
)
[time] => 10.52
)
[1] => Array
(
[query] => select `users`.*, `followers`.`follower_id` as `pivot_follower_id`, `followers`.`user_id` as `pivot_user_id` from `users` inner join `followers` on `users`.`id` = `followers`.`user_id` where `followers`.`follower_id` = ?
[bindings] => Array
(
[0] => 105
)
[time] => 0.34
)
[2] => Array
(
[query] => select count(*) as aggregate from `statuses` where `user_id` in (?, ?, ?, ?)
[bindings] => Array
(
[0] => 101
[1] => 102
[2] => 3
[3] => 105
)
[time] => 0.28
)
[3] => Array
(
[query] => select * from `statuses` where `user_id` in (?, ?, ?, ?) order by `created_at` desc limit 30 offset 120
[bindings] => Array
(
[0] => 101
[1] => 102
[2] => 3
[3] => 105
)
[time] => 1.3
)
这里证明非with照样可以取出用户数据而且少了一条sql,作者能帮忙解释下么
来拆解一下教程中的查询语句
假设 user_ids = [1,2,3]
并且status
表中有100条数据
Status::whereIn('user_id', $user_ids)->orderBy('created_at','desc');
如果不带with('user')
,最终执行的sql语句如下
select * from `statuses` where `user_id` in (1, 2, 3) order by `created_at` desc
这个时候,我们来循环打印出所有status
关联的user
的id
$user_ids = [1,2,3];
$statuses = Status::whereIn('user_id', $user_ids)->orderBy('created_at','desc')->get();
foreach($statuses as $status)
{
echo $status->user->id;
}
由于在status
模型中定义的user
方法,这时候会向数据库提交请求,查询users
表,最终执行的sql代码如下
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 3 limit 1
select * from `users` where `users`.`id` = 2 limit 1
.
.
.
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 3 limit 1
select * from `users` where `users`.`id` = 2 limit 1
总计执行了101次
再次加上with('user')
查询看一下
Status::whereIn('user_id', $user_ids)->with('user')->orderBy('created_at','desc');
最终执行的sql语句如下
select * from `statuses` where `user_id` in (1, 2, 3) order by `created_at` desc
select * from `users` where `users`.`id` in (1, 2, 3)
在查询status
表的同时,也查询出了 users
表中的所有user_ids
关联的数据
再次循环打印出所有status
关联的user
的id
foreach($statuses as $status)
{
echo $status->user->id;
}
最终执行的sql语句如下
select * from `statuses` where `user_id` in (1, 2, 3) order by `created_at` desc
select * from `users` where `users`.`id` in (1, 2, 3)
发现sql语句没有变化,并没有向数据库查询,因为status
关联的user
字段已经被查询出来了
这里之所以使用with
关联,是因为视图中也使用了status->user
方法,可以想象,如果没有使用with('user')
关联同时查询出users
表中的相关用户,最终的sql查询是多么可怕。
推荐文章: