从一个表中选择所有不在Laravel 5.1中的表中存在的记录 [英] Select all records from one table that do not exist in another table in Laravel 5.1

查看:61
本文介绍了从一个表中选择所有不在Laravel 5.1中的表中存在的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个表中获取所有记录,这些记录在Laravel 5.1的其他表中不存在.

I want to get all the records from a table which do not exist in other table in Laravel 5.1.

我知道如何在核心php中执行此操作,并且可以在以下代码中正常工作

I know how to do this in core php, and it works fine with the following code

SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL

模型

public function audiences() 
{ 
return $this->belongsTo('App\BridalRequest', 'request_id'); 
}

但是当我尝试通过使用以下代码在Laravel中做同样的事情时,

but when i try to do the same in Laravel by using the following code,

$all_bridal_requests_check = \DB::table('bridal_requests')
                    ->where(function($query)
                    {
                        $query->where('publisher', '=', 'bq-quotes.sb.com')
                              ->orWhere('publisher', '=', 'bq-wd.com-bsf');
                    })
                    ->whereNotIn('id', function($query) { $query->table('audiences')->select('request_id'); })
                    ->orderBy('created_on', 'desc')
                    ->get();

然后它给我这个错误

调用未定义的方法Illuminate \ Database \ Query \ Builder :: table()

Call to undefined method Illuminate\Database\Query\Builder::table()

推荐答案

可以使用laravel查询构建器以以下方式构建上述查询.

The above mentioned query can be built using laravel query builder in the following manner.

SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL

这等效于使用Laravel的查询生成器构建的以下查询.

This is equivalent to below query built using Laravel's query builder.

\DB::table('table1 AS t1')
->select('t1.name')
->leftJoin('table2 AS t2','t2.name','=','t1.name')
->whereNull('t2.name')->get();

这篇关于从一个表中选择所有不在Laravel 5.1中的表中存在的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆