【问题标题】:Laravel : Search by min and max value from the tableLaravel:按表中的最小值和最大值搜索
【发布时间】:2018-12-23 21:25:01
【问题描述】:

我对使用 min-max 值的搜索感到困惑。在我的 posts 表中有两个字段 min_pricemax_price,在我的搜索中,我需要在搜索查询中涵盖几件事.

  1. 如果用户只搜索max_value,则会显示所有价格为less than or equal to max_value的帖子。

  2. 如果用户只搜索min_value,则会显示所有价格为less than or equal to min_value的帖子。

  3. 如果用户搜索min_valuemax_value,它会显示所有价格在min_value and max_value之间的帖子。

  4. 如果两者都为空,则返回所有帖子。

我该怎么做?

我的代码:

$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();

我该怎么做

【问题讨论】:

  • 对于min_value 标准相同小于或等于 min_value 你确定吗?我想它应该大于或等于
  • @MKhalidJunaid 对于案例 2 ?
  • 第二种情况是的
  • @MKhalidJunaid 是,如果仅在 min_price 内搜索

标签: laravel laravel-5.5


【解决方案1】:

检查:
1.是否两者(最小值和最大值)都可用(即不为空):
2. strong> 如果最小值可用:
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_pricemax_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);
}

【讨论】:

  • on case:1 如果我同时拥有min_valuemax_value 的值怎么办?我应该将其与仅价格进行比较吗?因为我的 table.min_price 和 max_price 中有两个不同的字段
  • 这是客户要求。
  • 您的意思是价格共有 3 个字段:(price, min_price & max_price) ???
  • 在我的帖子表中有两个字段 min_price 和 max_price。我没有谈论“价格”。
【解决方案2】:

你可以设置 $min = 0;和 $max = 无限选择数;并将 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();

参考:https://laravel.com/docs/5.6/queries

【讨论】:

    【解决方案3】:

    你不能用 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 可能会失败,但这是这样做的方法。

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      相关资源
      最近更新 更多