Laravel 中的数字范围交叉搜索

我有来自2 列的记录 用户可以通过选择或输入ant值来过滤结果如何使用 Laravel 正确实现这个东西。我现在拥有的很简单

$query->where("field_to", '<=', $field_to);
$query->where("field_from", '>=', $field_from);

但这将不起作用,例如,如果数据库中的值是 10 和 100,但用户输入的是 50 和 200。我需要某种交叉检查。或者我过度设计了什么?



精慕HU
浏览 164回答 1
1回答

万千封印

你必须使用这样的东西:$field_to = 50;$field_from = 200;$query->where(function ($query) use ($field_from) {&nbsp; &nbsp; $query->where('field_from', '<=', $field_from)&nbsp; &nbsp; &nbsp; &nbsp; ->where('field_to', '>=', $field_from);})->orWhere(function ($query) use ($field_to) {&nbsp; &nbsp; $query->where('field_from', '<=', $field_to)&nbsp; &nbsp; &nbsp; &nbsp; ->where('field_to', '>=', $field_to);});这将抓住你的场景。例如,如果数据库中的记录是 50 和 100,并且用户输入这些值:55 和 70:抓住70 和 150:接住150 和 200:未捕获20 和 30:未捕获20 和 75:抓住
打开App,查看更多内容
随时随地看视频慕课网APP