【问题标题】:Laravel DB Query Builder "whereNotIn" to another tableLaravel DB 查询生成器“whereNotIn”到另一个表
【发布时间】:2016-10-28 10:45:27
【问题描述】:

如何在不使用 DB::raw() 的情况下使用查询生成器将 whereNotIn 设置为另一个表;

$query ="select * from project 
            where prj_usr_id= $user->id 
            and now()<prj_expiry 
            and prj_id not in(select bd_prj_id from bid where bd_status=1) 
            and prj_status='open' 
            order by prj_updated_date desc;

【问题讨论】:

标签: database laravel


【解决方案1】:

我解决了这个问题。

$results = DB::table('project')
                        ->where('prj_usr_id',  '=' , 1 ) 
                        ->where('prj_status',  '=' , 'open' ) 
                        ->where('prj_expiry',  '<' , Carbon::Now() ) 
                        ->whereNotIn ('prj_id',function ($query)
                        {
                            $query->select(DB::raw(1))
                                    ->from('bid')
                                    ->where('bd_status',  '=' , '1' ) 
                                    ->get(['bd_prj_id']); 
                        })
                        ->orderBy('prj_updated_date', 'desc') 
                        ->paginate(5);
return  $results; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2013-09-13
    • 2016-03-14
    • 2020-05-02
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多