A.A Noman
A.A Noman

Reputation: 5270

Why does not work whereBetween in laravel

I have a model named Student and this model contains some field

id teacher_id roll_number
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
6 1 6
7 1 7
8 1 8
9 1 9
10 1 10
11 2 11
12 2 12
13 2 13

I have a form that passes values teacher_id, start_roll and end_roll

I have a query where I use whereBetween method to get student information

$studentData = Student::where('teacher_id',$request->teacher_id)->whereBetween('roll_number',[$request->start_roll,$request->end_roll])->get();

where start_roll value is 1 and end_roll value is 10 and teacher_id value is 1 but it is strange it only return two array values where id 1 and 10 only

Again if I search start_roll value is 5 and end_roll value is 10 and teacher_id value is 1 but it also strange it returns an empty array

If I print the above query when search it prints like below

Student::where('teacher_id',1)->whereBetween('roll_number',[5,10])->get();

If I execute the above query hardcoded(entry roll_number manually from 5 to 10) then it works fine for me and it returns 6 number of the array where id start 5 and end 10

I don't know why my search is not working. Any suggestion is appreciate please

Upvotes: 2

Views: 126

Answers (1)

Zrelli Majdi
Zrelli Majdi

Reputation: 1270

You should add all requested values 1,2,3,4,5,6,7,8,9,10. Like This:

$queryRolls = range($request->start_roll,$request->end_roll);
$studentData = Student::where('teacher_id',$request->teacher_id)->whereIn('roll_number',$queryRolls)->get();

Upvotes: 1

Related Questions