[英]How to find value within a range - laravel
I have two columns in a table like below 我在如下表中有两列
id start end point
1 50 70 5
2 80 100 7
How can find the value's range in my code below.. Now when i submit the mark (70), i want to find the range it belongs to so i can get the respective points. 如何在下面的代码中找到该值的范围。现在,当我提交标记(70)时,我想找到它所属的范围,这样我就可以获得相应的分数。
lets say $marks = 70
public function getGrade($marks)
{
$grade_point = Grade::where(function ($query) use ($marks) {
$query->where('from', '<=', $marks);
$query->where('to', '>=', $marks);
return $grade_point->point;
}
With the code above i get the error 与上面的代码我得到错误
Object of class Illuminate\\Database\\Eloquent\\Builder could not be converted to string 无法将Illuminate \\ Database \\ Eloquent \\ Builder类的对象转换为字符串
You have to call ->first()
, ->get()
or ->all()
to execute to the query: 您必须调用->first()
, ->get()
或->all()
来执行查询:
// lets say $marks = 70
public function getGrade($marks)
{
$grade_point = Grade::where(function ($query) use ($marks) {
$query->where('from', '<=', $marks);
$query->where('to', '>=', $marks);
})->first();
return $grade_point->point;
}
Checkout the docs: https://laravel.com/docs/5.8/queries#retrieving-results 检出文档: https : //laravel.com/docs/5.8/queries#retrieving-results
You need to send the query with a method like get()
, first()
, pluck('column')
, value('column')
.... 您需要使用get()
, first()
, pluck('column')
, value('column')
...之类的方法发送查询。
lets say $marks = 70
public function getGrade($marks)
{
$grade_point = Grade::where('from', '<=', $marks)->where('to', '>=', $marks);
return (int)$grade_point->value('point');
}
the method value('column')
returns the column's value of the firsts result or null. 方法value('column')
返回第一个结果的列值或null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.