【问题标题】:Convert SQL query into Laravel query builder将 SQL 查询转换为 Laravel 查询构建器
【发布时间】:2018-08-14 14:47:30
【问题描述】:

我需要将此查询转换为 laravel 查询生成器

select * from employee where(( age = 25 and salary = 20000) or (age =30 and salary = 30000))

【问题讨论】:

  • 你目前的尝试是什么?

标签: laravel laravel-query-builder


【解决方案1】:

如果你想对 where 子句进行分组,你可以将它们嵌套在闭包中:

DB::table('employee')
    ->where(function ($query) {
        $query->where('age', 25)->where('salary', 20000);
    })
    ->orWHere(function ($query) {
        $query->where('age', 30)->where('salary', 30000);
    })
    ->get();

有关更多信息,请查看文档中的Parameter Grouping

【讨论】:

  • 你能给我一个参考或文档链接吗?
【解决方案2】:

你可以试试这个,

$data = Model::where([["age", "25"],["salary", "20000"]])
    ->orWhere([["age", "30"],["salary", "30000"]])
    ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 2021-08-19
    • 2021-12-11
    • 2015-07-12
    • 2017-04-07
    • 1970-01-01
    • 2020-02-07
    • 2016-02-11
    相关资源
    最近更新 更多