【发布时间】:2020-01-13 20:47:51
【问题描述】:
我的代码中有公共数据和用户特定数据之间的联合。我想要实现的是,如果没有用户登录,我返回 public 为 true 的数据。如果我有一个用户,我会进行另一个查询,其中 user_id 是登录用户。一切正常,直到我想获得不应该被允许的用户的特定数据 ID。
例如我有数据:
[
'id' => 1,
'user_id' => 1,
'public' => true,
],
[
'id' => 2,
'user_id' => 1,
'public' => false,
],
我当前的代码:
public function getQuery() : Builder
{
$publicData = $this->model->where('public', true);
// $this->user is passed thought another method which is $request->user() result.
if (!isset($this->user)) {
return $publicData;
}
if ($this->user->isAdmin()) {
return $this->model->newQuery();
}
return $this->model
->where('user_id', $this->user->id)
->union($publicData);
}
现在我们假设 $this->user->id 为 10,我尝试获取 id 不允许的数据。
$data = $this->getQuery()
->where('id', 2)
->first();
在这种情况下,总是会返回第一个公共数据,在这种情况下是 id 1,我希望收到 null。
我不确定如何找到解决方案,也不确定我缺少什么。目前我使用 Laravel 6
【问题讨论】: