laravel 的搜索中的括号应该怎么加

有需要搜索status为1或2且 action为1的记录,原生的sql是where (status_id=1 or status_id=2) and action_id = 1,但是用
orWhere的话是这样的 Where(‘status_id’,1)->orWhere(‘status_id’,2)->where(‘action_id’,1),这样搜索的结果实际是
where status_id = 1 or status_id=2 and action_id = 1,和需要的不符,问下这个括号在组合中怎么处理?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案
$sql = DB::table('table_name')
     ->where(function ($sql) {
        $sql->where('status_id', '=', 1)->orWhere('status_id', '=', 2);
    })
    ->where('action_id',1);

where 部分这样写试一下。

4年前 评论
讨论数量: 4
leo

课程买了是要看的,而不是当收藏品

4年前 评论
$sql = DB::table('table_name')
     ->where(function ($sql) {
        $sql->where('status_id', '=', 1)->orWhere('status_id', '=', 2);
    })
    ->where('action_id',1);

where 部分这样写试一下。

4年前 评论

whereIn('status_id',[1,2])->where('action',1)

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!