You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel访问器中实现多条件状态转换替代DB::raw?

解决方案

一、使用模型访问器实现

在LeaveApplication模型中定义访问器,自动将status字段转换为对应的状态文本:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class LeaveApplication extends Model
{
    // 状态映射表
    private $statusMap = [
        0 => 'Pending',
        1 => 'Approved',
        2 => 'Declined'
    ];

    // 定义访问器,自动生成application_status属性
    public function getApplicationStatusAttribute()
    {
        // 兜底返回Unknown避免无匹配值时出错
        return $this->statusMap[$this->status] ?? 'Unknown';
    }
}

之后查询无需再写DB::raw,直接查询基础字段即可,访问器会自动把application_status附加到模型实例中:

$leave_applications = LeaveApplication::select('leave_type', 'total_days', 'status')
    ->where('employee_id', $employee_id)
    ->get();

如果想让application_status始终出现在模型的数组/JSON输出中,可在模型里添加$appends属性:

class LeaveApplication extends Model
{
    protected $appends = ['application_status'];

    // ... 其他代码
}

二、Laravel风格的查询构造器写法(无需访问器)

Laravel 9及以上版本提供了case查询构造器方法,可链式构建CASE逻辑,避免手写冗长的原生SQL:

$leave_applications = LeaveApplication::select('leave_type', 'total_days')
    ->case('status')
        ->when(0, 'Pending')
        ->when(1, 'Approved')
        ->when(2, 'Declined')
        ->as('application_status')
    ->where('employee_id', $employee_id)
    ->get();

也可以用更紧凑的selectRaw写法简化原生CASE语句:

$leave_applications = LeaveApplication::select(
    'leave_type',
    'total_days',
    DB::raw("
        CASE status
            WHEN 0 THEN 'Pending'
            WHEN 1 THEN 'Approved'
            WHEN 2 THEN 'Declined'
        END AS application_status
    ")
)->where('employee_id', $employee_id)->get();

三、使用枚举类型(Laravel 8+)

Laravel 8及以上支持枚举字段,可定义枚举类统一管理状态:

namespace App\Enums;

enum LeaveStatus: int
{
    case Pending = 0;
    case Approved = 1;
    case Declined = 2;

    // 定义获取状态文本的方法
    public function label(): string
    {
        return match($this) {
            self::Pending => 'Pending',
            self::Approved => 'Approved',
            self::Declined => 'Declined',
        };
    }
}

在LeaveApplication模型中把status字段类型指定为枚举:

class LeaveApplication extends Model
{
    protected $casts = [
        'status' => LeaveStatus::class,
    ];
}

查询时直接通过枚举的label()方法获取状态文本:

$leave_applications = LeaveApplication::select('leave_type', 'total_days', 'status')
    ->where('employee_id', $employee_id)
    ->get();

// 使用示例
foreach ($leave_applications as $app) {
    $statusText = $app->status->label();
}

内容的提问来源于stack exchange,提问作者prajwal-medibhai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 17:03:34