在 Laravel 中向查询构建器添加键

Add a key to a query builder in Laravel

我想知道是否可以添加一个不是 table 列的键。

查询代码:

$list = DB::table('ouin_request_base as base')
->select(
    'base.request_id',
    'users.last_name as last_name',
    'users.first_name as first_name',
    'division.name as division_name',
    'base.created_at',
    'status.name as status_name'
)
->join('users', 'base.user_id' , '=', 'users.id')
->join('ouin_request_data as data', 'base.request_id', '=', 'data.request_id')
->join('m_request_status as status', 'base.request_state', '=', 'status.ouin')
->join('m_divisions as division', 'users.division_id', '=', 'division.id')
->when(isset($search_name), function($query) use ($search_name){
    return $query->where('users.id', '=', $search_name);
})
->when(isset($search_division), function($query) use ($search_division){
    return $query->where('division.id', '=', $search_division);
})
->when(isset($search_status), function($query) use ($search_status){
    return $query->where('status.ouin', '=', $search_status);
})
->paginate(2);

查询生成器结果:

 #items: array:2 [▼
  0 => {#1356 ▼
    +"request_id": "1-20210802142739"
    +"last_name": "last_name"
    +"first_name": "first_name"
    +"division_name": "OA"
    +"created_at": "2021-08-02 14:27:39"
    +"status_name": "state"
  }
  1 => {#1427 ▼
    +"request_id": "1-20210802171508"
    +"last_name": "last_name"
    +"first_name": "first_name"
    +"division_name": "OA"
    +"created_at": "2021-08-02 17:15:08"
    +"status_name": "state"
  }
]

我想添加的是检查当前登录用户是否可以通过检查 if(Auth::user()->manager_rank == $form->approve_state)(manager_rank 和 approve_state 是整数)来批准这些表单并添加 'can_approve' 分页前集合的键。 $form 是 'ouin_request_base' table 中各个表单的示例。因此,如果用户的 manager_rank 等于个人表单的状态,则 can_approve 将为真。

示例:

#items: array:2 [▼
  0 => {#1356 ▼
    +"request_id": "1-20210802142739"
    +"last_name": "last_name"
    +"first_name": "first_name"
    +"division_name": "OA"
    +"created_at": "2021-08-02 14:27:39"
    +"status_name": "state"
    +"can_approve": "true"
  }
  1 => {#1427 ▼
    +"request_id": "1-20210802171508"
    +"last_name": "last_name"
    +"first_name": "first_name"
    +"division_name": "OA"
    +"created_at": "2021-08-02 17:15:08"
    +"status_name": "state"
    +"can_approve": "false"
  }
]

是否可以在分页之前使用when子句将'can_approve'添加到集合中?

您可以直接在查询中执行此操作:

select(
    'base.request_id',
    'users.last_name as last_name',
    'users.first_name as first_name',
    'division.name as division_name',
    'base.created_at',
    'status.name as status_name',
    DB::raw('... AS can_approve')
)

如果你有一个 eloquent 模型,你甚至可以通过 accessor.

来解决这个问题

您可以通过多种方式添加新的键和值,但按照您的自定义逻辑执行以下操作:

$list = DB::table('ouin_request_base as base')
->select(
    'base.request_id',
    'users.last_name as last_name',
    'users.first_name as first_name',
    'division.name as division_name',
    'base.created_at',
    'status.name as status_name'
)
->join('users', 'base.user_id' , '=', 'users.id')
->join('ouin_request_data as data', 'base.request_id', '=', 'data.request_id')
->join('m_request_status as status', 'base.request_state', '=', 'status.ouin')
->join('m_divisions as division', 'users.division_id', '=', 'division.id')
->when(isset($search_name), function($query) use ($search_name){
    return $query->where('users.id', '=', $search_name);
})
->when(isset($search_division), function($query) use ($search_division){
    return $query->where('division.id', '=', $search_division);
})
->when(isset($search_status), function($query) use ($search_status){
    return $query->where('status.ouin', '=', $search_status);
});

//Add custom keys here...
if(Auth::user()->manager_rank == $form->approve_state){
    $list = $list->addSelect(DB::raw("'true' as can_approve"));
}else{
    $list = $list->addSelect(DB::raw("'false' as can_approve"));
}

$list = $list->paginate(2);

你可以在分页后做这样的事情

foreach ($list as $item)
        {
            //do your logic here 
            $item->can_ approve = 'false';
        }

我试过了,它很有效,所以我要post我的代码在这里。

$list =
        DB::table('ouin_request_base as base')
        ->select(
            'base.request_id',
            'users.last_name as last_name',
            'users.first_name as first_name',
            'division.name as division_name',
            'base.created_at',
            'status.name as status_name',
            'base.request_state as can_approve'
        )
        ->join('users', 'base.user_id', '=', 'users.id')
        ->join('ouin_request_data as data', 'base.request_id', '=', 'data.request_id')
        ->join('m_request_status as status', 'base.request_state', '=', 'status.ouin')
        ->join('m_divisions as division', 'users.division_id', '=', 'division.id')
        ->when(isset($search_name), function ($query) use ($search_name)
        {
            return $query->where('users.id', '=', $search_name);
        })
        ->when(isset($search_division), function ($query) use ($search_division)
        {
            return $query->where('division.id', '=', $search_division);
        })
        ->when(isset($search_status), function ($query) use ($search_status)
        {
            return $query->where('status.ouin', '=', $search_status);
        })->paginate(2);

        foreach($list as $item)
        {
            $item->can_approve = Auth::user()->manager_rank == $item->can_approve ? true : false;
        }

我将 'base.request_state as can_approve' 添加到 select 查询以获取个人表单 request_state,这是一个整数。然后在查询之外,我循环列表并检查当前登录用户的 manager_rank 是否与个人表单的 request_state 相同。