Search code examples
phplaravellaravel-query-builder

How to check whether a number lies between 2 values using Laravel query


I have to trigger a mail when student crosses several stages . I have two tables

  1. studentreward_point_categories and
  2. student_reward_points.

If any student reaches a stage then need to send a mail. How to get category from db .

Reward point Category table.

enter image description here

  1. Student reward point table.

enter image description here

If for student_id = 19 have 345 points How to get his reward category. i have tried below code.

$total_point = StudentRewardPoint::where('student_id', $request->student_id)
    ->sum('points');
if(!empty($total_point)){
    return $pointCategory = RewardPointCategory::where('from_value','>=', $total_point)
        ->where('to_value','<=',$total_point)
        ->where('status',1)
        ->first();
}

Using this query I'm not able to get user reward point category.


Solution

  • You are querying it totally wrong!! From my point of view swap your '<=' and '>='

    return $pointCategory = RewardPointCategory::where('from_value','<=',$total_point)- 
        >where('to_value','>=',$total_point)->where('status',1)->first();