2

I have two columns in my table: max and current. I want to build simple scope

public function scopeNotMax($query)
{
    return $query->where('max', '>', 'current');
}

But Laravel gives me that query:

SELECT * FROM table WHERE `max` > 'current'

I don't want this result and I know that I can use in this place whereRaw() or DB::raw(). But I can't find another way to say "hey, this is column, not string!'. Can I do it? Or I must use raws? I want to avoid it.

ventaquil
  • 2,780
  • 3
  • 23
  • 48
  • 1
    Even in the Laravel's doc it used `whereRaw` when it needed this https://laravel.com/docs/5.1/queries#advanced-where-clauses look at the Exists Statements section. – Positivity Feb 11 '16 at 12:16

3 Answers3

1

There is no other way.

where() method in this case add third parameter (value) to bindings and passes it ot PDO library. So it will be escaped.

Alternatively You can pass as third parameter a Closure, but then laravel will form a sub-select for You, which doesn't helps much.

Looks like whereRaw() is made for this kind of sitiuation.

D. Cichowski
  • 777
  • 2
  • 7
  • 24
0

Did you give a try with this ? return $query->where('max > current');

  • 1
    I found this one for you! http://stackoverflow.com/questions/30331437/laravel-eloquent-compare-column-values – Vijay Sai Chaudary Feb 11 '16 at 11:01
  • The answer can become more valuable if you added some explanation and links to documentation, so the original poster and other users can actually learn from it. – Markus Safar Feb 11 '16 at 16:57
0

you can use whereRaw():

->whereRaw('table_1.name = table_2.name');

You exmaple code:

->whereRaw('max>current');