[英]How to select all the values from a table except the values which are in foreign table?
I have three tables: tbl_borrower, tbl_client
and tbl_guarantor
我有三个表:
tbl_borrower, tbl_client
和tbl_guarantor
tbl_Client:
id|name|address|email|
tbl_Guarantor:
id|clientid(fk)|Guaranteed_date|borrower_id(fk from borrower table)
I want to retrieve all the values of client table except the values which are present in guarantor table in the controller of Laravel 5.5
. 我想检索客户端表的所有值,除了
Laravel 5.5
控制器中的保证表中存在的值。
Once you have the models and relationships set up, you should just do: 一旦你建立了模型和关系,你应该这样做:
Client::doesntHave('guarantor')->get()
https://laravel.com/docs/5.6/eloquent-relationships#querying-relationship-absence https://laravel.com/docs/5.6/eloquent-relationships#querying-relationship-absence
If you're using the query builder, it would be: 如果您正在使用查询构建器,那么它将是:
$clients = DB::table('tbl_clients')
->join('tbl_guarantor', 'tbl_clients.id', '=', 'tbl_guarantor.clientid')
->select('tbl_clients.*')
->whereNull('tbl_guarantor.clientid')
->get();
https://laravel.com/docs/5.5/queries https://laravel.com/docs/5.5/queries
With the premise of using a left join and testing for NULL on the 2nd table id based on this answer. 基于此答案,在第二个表id上使用左连接和测试NULL的前提下。 https://stackoverflow.com/a/4076157/3585500
https://stackoverflow.com/a/4076157/3585500
Try this : 尝试这个 :
DB::table('tbl_Client')
->groupBy('tbl_Client.id')
->leftjoin('tbl_Guarantor', 'tbl_Client.id', '=', 'tbl_Guarantor.clientid')
->get([
'tbl_Client.*'
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.