Laravel : 按 table 中的最小值和最大值搜索
Laravel : Search by min and max value from the table
我对使用 min-max
value.In 进行搜索感到困惑 我的 posts
table 有两个字段 min_price
和 max_price
,在我的搜索有几件事我需要在搜索查询中涵盖。
如果用户只搜索max_value
,它会显示价格为less than or equal to max_value
的所有帖子。
如果用户只搜索min_value
,它会显示价格为less than or equal to min_value
的所有帖子。
如果用户搜索min_value
和max_value
,它会显示价格在min_value and max_value
.
[=45=之间的所有帖子]
如果均为空,return 所有帖子。
我该怎么做?
我的代码:
$searchablePost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereIn('product_id', $userApprovalProductIDs)
->whereIn('demand_or_supply', $demand_or_supply);
// skip my search query code
$searchedPost = $searchablePost->offset($offset)->limit($limit)->orderBy('id','desc')->get();
我该怎么做
你不能用 whereIn 做到这一点,你可以用 where 语句做到这一点。
像这样
`
$searchablePost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereIn('product_id', $userApprovalProductIDs)
->whereIn('demand_or_supply', $demand_or_supply)
->where('price', '>=', $minPrice)
`
没有尝试过,所以 int 可能会失败,但这是方法。
检查:
1. 如果两者(最小值和最大值)都可用(即不为空):
2. 如果最小值可用:
3. 如果最大值可用:
// if none of them is null
if (! (is_null($min_value) && is_null($max_value))) {
// fetch all between min & max values
$searchablePost = $searchablePost->whereBetween('price', [$min_value, $max_value]);
}
// if just min_value is available (is not null)
elseif (! is_null($min_value)) {
// fetch all greater than or equal to min_value
$searchablePost = $searchablePost->where('price', '>=', $min_value);
}
// if just max_value is available (is not null)
elseif (! is_null($max_value)) {
// fetch all lesser than or equal to max_value
$searchablePost = $searchablePost->where('price', '<=', $max_value);
}
如果您有 min_price
和 max_price
的单独字段,如评论中所述,只需更改代码如下:
if (! (is_null($min_value) && is_null($max_value))) {
$searchablePost = $searchablePost
->where('min_price', '>=', $min_value)
->where('max_price', '<=', $max_value);
}
elseif (! is_null($min_value)) {
$searchablePost = $searchablePost->where('min_price', '>=', $min_value);
}
elseif (! is_null($max_value)) {
$searchablePost = $searchablePost->where('max_price', '<=', $max_value);
}
你可以设置$min = 0;和 $max = infinite_choosen_number;并将 whereBetween 方法附加到您的查询中,如以下代码:
$searchablePost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereIn('product_id', $userApprovalProductIDs)
->whereIn('demand_or_supply', $demand_or_supply)
->whereBetween('price', ["$min", "$max"])->get();
我对使用 min-max
value.In 进行搜索感到困惑 我的 posts
table 有两个字段 min_price
和 max_price
,在我的搜索有几件事我需要在搜索查询中涵盖。
如果用户只搜索
max_value
,它会显示价格为less than or equal to max_value
的所有帖子。如果用户只搜索
min_value
,它会显示价格为less than or equal to min_value
的所有帖子。如果用户搜索
[=45=之间的所有帖子]min_value
和max_value
,它会显示价格在min_value and max_value
.如果均为空,return 所有帖子。
我该怎么做?
我的代码:
$searchablePost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereIn('product_id', $userApprovalProductIDs)
->whereIn('demand_or_supply', $demand_or_supply);
// skip my search query code
$searchedPost = $searchablePost->offset($offset)->limit($limit)->orderBy('id','desc')->get();
我该怎么做
你不能用 whereIn 做到这一点,你可以用 where 语句做到这一点。 像这样
`
$searchablePost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereIn('product_id', $userApprovalProductIDs)
->whereIn('demand_or_supply', $demand_or_supply)
->where('price', '>=', $minPrice)
`
没有尝试过,所以 int 可能会失败,但这是方法。
检查:
1. 如果两者(最小值和最大值)都可用(即不为空):
2. 如果最小值可用:
3. 如果最大值可用:
// if none of them is null
if (! (is_null($min_value) && is_null($max_value))) {
// fetch all between min & max values
$searchablePost = $searchablePost->whereBetween('price', [$min_value, $max_value]);
}
// if just min_value is available (is not null)
elseif (! is_null($min_value)) {
// fetch all greater than or equal to min_value
$searchablePost = $searchablePost->where('price', '>=', $min_value);
}
// if just max_value is available (is not null)
elseif (! is_null($max_value)) {
// fetch all lesser than or equal to max_value
$searchablePost = $searchablePost->where('price', '<=', $max_value);
}
如果您有 min_price
和 max_price
的单独字段,如评论中所述,只需更改代码如下:
if (! (is_null($min_value) && is_null($max_value))) {
$searchablePost = $searchablePost
->where('min_price', '>=', $min_value)
->where('max_price', '<=', $max_value);
}
elseif (! is_null($min_value)) {
$searchablePost = $searchablePost->where('min_price', '>=', $min_value);
}
elseif (! is_null($max_value)) {
$searchablePost = $searchablePost->where('max_price', '<=', $max_value);
}
你可以设置$min = 0;和 $max = infinite_choosen_number;并将 whereBetween 方法附加到您的查询中,如以下代码:
$searchablePost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereIn('product_id', $userApprovalProductIDs)
->whereIn('demand_or_supply', $demand_or_supply)
->whereBetween('price', ["$min", "$max"])->get();