ventaquil
ventaquil

Reputation: 2848

Using two columns in where Laravel ORM

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.

Upvotes: 2

Views: 519

Answers (3)

Sagar Sharma
Sagar Sharma

Reputation: 33

you can use whereRaw():

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

You exmaple code:

->whereRaw('max>current');

Upvotes: 0

D. Cichowski
D. Cichowski

Reputation: 777

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.

Upvotes: 1

Vijay Sai Chaudary
Vijay Sai Chaudary

Reputation: 177

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

Upvotes: 0

Related Questions