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 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案
$sql = DB::table('table_name')
     ->where(function ($sql) {
        $sql->where('status_id', '=', 1)->orWhere('status_id', '=', 2);
    })
    ->where('action_id',1);

where 部分这样写试一下。

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

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

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

where 部分这样写试一下。

5年前 评论

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

5年前 评论

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