Search code examples
laravel-5.4

Calculation in laravel eloquent query


I have a table with 2 columns and I want to get the columns that when the difference is made, the answer is greater than 500 Like this:

colOne - colTow

Can I do something like this is laravel eloquent

return $query->where('colOne','-','colTow','>=500')->get();

Solution

  • You can do this in two ways:

    ->whereRaw('colOne - colTow >= ?',[500])
    or
    ->where(Db::raw('colOne - colTow') , '>=' ,500)

    Read this: Raw Expressions.