【问题标题】:Laravel Eloquent Query Using WHERE with AND orWhereLaravel Eloquent 查询使用 WHERE 和 AND orWhere
【发布时间】:2020-01-01 18:30:00
【问题描述】:

我是 Laravel 的初学者。在我的项目 Laravel 5.8 中使用。 我有这个数组:

array:4 [
  0 => 1
  1 => 3
  2 => 4
  3 => 5
]

和功能:

public function getPaymentMethods(array $paymentId)
    {
            dd($paymentId);
            return PaymentSettings::where('id', '=', $paymentId)->get();
    }

它不起作用,因为在 $paymentId 我有数组。 如何更改我的代码函数以与数组一起使用(许多 where/orWhere)?

我需要这样的东西(但在 laravel 雄辩):

SELECT * from payment_settings where id = 1 or id = 3 or id = 4 or id = 5.

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    你需要whereIn,

    return PaymentSettings::whereIn('id', $paymentId)->get();
    

    注意: whereIn 方法验证是否包含给定列的值 在给定的数组内:

    您可以查看详细文档here。

    【讨论】:

      【解决方案2】:

      where 子句不用于从数组中搜索,whereIn 用于。 用法-

      return PaymentSettings::whereIn('id', $paymentId)->get();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-22
        • 2021-07-12
        • 2021-11-06
        • 2018-01-30
        • 1970-01-01
        • 2021-02-11
        • 2020-06-27
        • 2020-08-03
        相关资源
        最近更新 更多