在Eloquent中获得奇数或偶数ID

时间:2017-05-23 08:24:36

标签: eloquent lumen

我有一个任务需要显示每个类型的假日,其中id是奇数。当我在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();
}

1 个答案:

答案 0 :(得分:1)

您可以使用raw expression

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