Home > Blockchain >  Eloquent Get from Table A where it has one relation in B
Eloquent Get from Table A where it has one relation in B

Time:07-03

I'm trying to get the records from Table A that has one and only one record in Table B, I'm not interested in records that have no records in B nor that have multiple records in B, I tried using

TableA::whereHas('tableB')->get()

This didn't return A's where no records in B but I still don't need ones that have multiple in B

CodePudding user response:

use has so you can restrict query to fetch only those record which has one record in tableB model

TableA::has('tableB', '=', 1)->get()
  • Related