在 Eloquent 中获取奇数或偶数 ID

Getting Odd or Even ID in Eloquent

我有一个任务需要显示每个 ID 为奇数的假期类型 number.And 当我尝试在 Eloquent 中执行它时它总是给我一个错误。

查询

Select holiday_type.id, holiday_type.name
From holiday_type
Where (holiday_type.id % 2) = 0;

Laravel PHP

return DB::table('holiday_type')
       ->select('holiday_type.id','holiday_type.name as text')
    // ->where(('id%2'), '=', 0)) ** I also tried different format but still nothing 
       ->get();
}

您可以使用raw expression

DB::table('holiday_type')
    ->select(DB::raw('holiday_type.id, holiday_type.name as text'))
    ->whereRaw('MOD(id, 2) = 0')
    ->get();