Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hanif-king's avatar

How to get all data in table A where not has a foreign key in it's child table B and unlike same relation

i have 3 tables tbl_Transporters , tbl_Transactions and tbl_TransactionStatus where table tbl_transporter has (hasMany) relationship with tbl_transaction and tbl_Transaction has (belongsTo) also tbl_Transporter has (hasOne) relations with tbl_Transaction and tbl_Transaction (belongsTo).

what i want is:

with ID of tbl_Transporters which i have , how can i get all those Transactions from tbl_Transactions where it not exist in tbl_TransactionStatus. and in second query all those transactions from tbl_Transactions where it exist in tbl_TransactionStatus .

please thank you.

0 likes
4 replies
burlresearch's avatar

Please post your "Transporter Model" so we can see your relation functions - I think that would help.

hanif-king's avatar

Transporter model

  public function transactions(){
        return $this->hasMany('App\Transaction');
    }

Transactions model

public function transporters(){
        return $this->belongsTo('App\Transporters');
    }

 public function transactionstatus(){
        return $this->hasOne('App\TransactionStatus','transaction_id');
    }

TransactionStatus

 public function transactions(){
        return $this->belongsTo('App\Transaction');
    }
burlresearch's avatar
Level 40

Here's my guess:

$xport       = Transport::find($transporter_id);
$with_status = $xport->transactions()->has('transactionstatus')->get();
$no_status   = xport->transactions()->doesntHave('transactionstatus')->get();
1 like

Please or to participate in this conversation.