T
T
tester_toster2018-03-27 17:39:39
Laravel
tester_toster, 2018-03-27 17:39:39

Laravel eloquent - how to create a relationship through two tables with a return?

There are tables and corresponding models:
wallets (Wallet)

id
...

transfer_wallets(TransferWallet) (pivot)
id
transfer_id
wallet_id

transfers (transfer)
transaction_id
from_wallet_id
to_wallet_id
...

transactions (Transaction)
id
.....

You need to get all transactions with transfers from Wallet.
Now there is a connection in the wallet:
public function transfers()
{
   return  $this->belongsToMany(Transfer::class, 'transfer_wallets', 'wallet_id', 'id')->with(['senderWallet', 'recipientWallet','transaction']);
}

It all looks like:
Wallet->Pivot->Transfers->Transaction
But it turns out ugly - because 1 transaction can have several transfers, and not vice versa.
How to implement a view link through relationships:
Wallet->(I can't figure out what should be here..)->Transactions->Transfers

I would appreciate any hints.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question