Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Odd or Even ID in Eloquent

Tags:

eloquent

lumen

I have a task that needs to be displayed every type of holiday where id is odd number.And when I tried to do it in Eloquent it always gives me an error.

Query

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 Answers

You can use raw expression

DB::table('holiday_type')
    ->select(DB::raw('holiday_type.id, holiday_type.name as text'))
    ->whereRaw('MOD(id, 2) = 0')
    ->get();
like image 143
krisanalfa Avatar answered Dec 01 '25 09:12

krisanalfa