gpt4 book ai didi

laravel - Laravel: Eloquent 动态where子句

转载 作者:行者123 更新时间:2023-12-03 10:32:31 29 4
gpt4 key购买 nike

我使用动态搜索参数调用URL。我怎样才能形成适当的 Eloquent 查询

理论上:

  • 查询
  • 查询where(someParam1)
  • 查询where(someParam2)
  • 查询orderby(someParam3)
  • 查询获取

  • 我需要这种结构,因此如果 参数存在,我可以使用where子句。
    如果Laravel还有其他方法,请告诉我。

    最佳答案

    Laravel很容易。只是做这样的事情:

    $query = User::query();

    if ($this == $that) {
    $query = $query->where('this', 'that');
    }

    if ($this == $another_thing) {
    $query = $query->where('this', 'another_thing');
    }

    if ($this == $yet_another_thing) {
    $query = $query->orderBy('this');
    }

    $results = $query->get();

    关于laravel - Laravel: Eloquent 动态where子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942556/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com