Laravel 当使用“查询”时,是否有方法为管理员添加异常;其中;在拉威尔?

Laravel 当使用“查询”时,是否有方法为管理员添加异常;其中;在拉威尔?,laravel,Laravel,所以我有一个查询,我用“where”过滤,只显示分配给特定用户的结果,代码如下: public function showAll() { return $this->where('owner_id' , Auth::id())->with([ 'contactType:id,name,class', 'owner:id,first_name,last_name', 'CreatedBy:id,first_name,last_

所以我有一个查询,我用“where”过滤,只显示分配给特定用户的结果,代码如下:

  public function showAll()
{
    return $this->where('owner_id' , Auth::id())->with([
        'contactType:id,name,class',
        'owner:id,first_name,last_name',
        'CreatedBy:id,first_name,last_name',
        'tags:id,name,color_code',
        'customFields',
        'persons',
        'country'
        // 'email.type',
        // 'phone.type'
    ]);

现在我想向id为(1)的管理员用户添加异常,以便id为1的用户可以播放所有结果。我有一个isAdmin和ID为1的baseuser,为什么不简单地将auth ID分配给var并检查它是否是一个呢?
有点像

public function showAll()
{
    $query = $this->with([
        'contactType:id,name,class',
        'owner:id,first_name,last_name',
        'CreatedBy:id,first_name,last_name',
        'tags:id,name,color_code',
        'customFields',
        'persons',
        'country'
    ]);
    
    $authId = Auth::id();
    if (1 !== $authId) {
        $query->where('owner_id' , $authId);
    }
    
    return $query;
}
您想在()时使用
。只有当条件为true时,才在内部添加查询

return $this->when(!$isAdmin, function($query) {
    $query->where('owner_id' , Auth::id());
  })
  ->with([
    'contactType:id,name,class',
    'owner:id,first_name,last_name',
    'CreatedBy:id,first_name,last_name',
    'tags:id,name,color_code',
    'customFields',
    'persons',
    'country'
    // 'email.type',
    // 'phone.type'
  ]);

在上面,当
时,它只会将
where()
添加到查询中$iAdmin
为真,即,
$iAdmin
为假。

我无法正确理解您的问题。我想你想显示id为1的所有结果,只需使用if语句…嘿,yeh iv尝试了类似的操作,但由于某些原因似乎不起作用,它只是为管理员显示相同的筛选结果too@VajaMania在调用
get()
方法之前,
$query->toSql()有什么作用
give you?我想isAdmin没有定义,但addmin id肯定是1。我不知道我到底做错了什么,它通过指定的id进行过滤,我想我需要在IF语句中更改一些内容,我会调查itAh,它起作用了。我将1作为字符串传递,因此它不起作用。非常感谢你,鲁尼