1

I have three tables:tbl_borrower, tbl_clientandtbl_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.

Raghbendra Nayak
  • 1,606
  • 3
  • 22
  • 47

3 Answers3

3

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

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
0

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

With the premise of using a left join and testing for NULL on the 2nd table id based on this answer. https://stackoverflow.com/a/4076157/3585500

ourmandave
  • 1,505
  • 2
  • 15
  • 49
0

Try this :

DB::table('tbl_Client')
      ->groupBy('tbl_Client.id')                           
      ->leftjoin('tbl_Guarantor', 'tbl_Client.id', '=', 'tbl_Guarantor.clientid')                           
      ->get([
           'tbl_Client.*'
       ]);
Leena Patel
  • 2,423
  • 1
  • 14
  • 28