Madan Adhikari
Madan Adhikari

Reputation: 81

Laravel 5.1 Eloquent whereBetween not working as expected

I have to get rows within a range of passed symbol no.

Image of Table from where query is to be done

Query:

Mark::select('users_id', 'symbol_no', 'mark_obtained')
        ->where('subject_trade_id', 2)
        ->whereBetween('symbol_no', [100, 1000])
        ->orderBy('symbol_no')
        ->get();

This query returns no data, but I was expecting total 9 rows form the query. If I dump the query, I find the query as expected.

Query Log Image

If I run the generated query to mysql then it woks fine.

Again, if I change the symbol no. range to something like this:

Mark::select('users_id', 'symbol_no', 'mark_obtained')
        ->where('subject_trade_id', 2)
        ->whereBetween('symbol_no', [10, 1011])
        ->orderBy('symbol_no')
        ->get();

It returns 2 rows this time, and this output is also wrong.

If I try changing symbol no. range and query like this:

Mark::select('users_id', 'symbol_no', 'mark_obtained')
        ->where('subject_trade_id', 2)
        ->whereBetween('symbol_no', [101, 200])
        ->orderBy('symbol_no')
        ->get();

Now it works fine as expected.

Upvotes: 1

Views: 849

Answers (2)

Anil K
Anil K

Reputation: 1

I faced same problem.. Could you please check "symbol_no" field type in database table, It should be number/integer.

Upvotes: 0

Madan Adhikari
Madan Adhikari

Reputation: 81

Found the problem

By mistake the symbol_no column was defined as varchar() which had to be int() so, whereBetween() was being unable to return expected data.

Upvotes: 2

Related Questions