whereBetween() Eloquent returning wrong results

2019-08-19 05:30发布

I have a Eloquent query and I am using query scope. I have a km column in my table and I am passing kmFrom and KmTo values as post data. Now I am doing a eloquent whereBetween and its returning wrong results when I search for km.

For example if I pass kmFrom = 1000 and kmTo = 2000. Its giving me results which are not in between these two values.

Eloquent:

Product::categoriesCategoryId($categoryId)
            ->km($kmFrom, $kmTo)->get();

Query Scope for the km:

 public function scopeKm($query, $kmFrom, $kmTo)
    {   
        if(($kmFrom) && ($kmTo)){
            return $query->whereBetween('km', ["$kmFrom", "$kmTo"]);   
        }
        return $query;
    }

Please assist what wrong I am doing.

1条回答
Fickle 薄情
2楼-- · 2019-08-19 06:03

It's different approach to doing same thing

public function scopeKm($query, $kmFrom, $kmTo)
    {   
        if(($kmFrom) && ($kmTo)){
            return $query->where('km','>' ,$kmFrom])->where('km','<',$kmTo);   
        }
        return $query;
    }

this will work same hope this will help you!

查看更多
登录 后发表回答