If what you want to achieve is this command:
App\User::find(1)->transactions->get();
Then why don't you structure your Database like:
-
companiestable => id | .... -
userstable => id | company_id | ... -
transactionstable => id | user_id | ...
Then add relations:
Company Model:
public function users() { return $this->hasMany(User::class); }
User Model:
public function company() { return $this->belongsTo(Company::class); }
public function transactions() { return $this->hasMany(Transaction::class); }
Transaction Model:
public function user() { return $this->belongsTo(User::class); }
Now you can have:
- Eager Load Relation:
$user = App\User::find(1)->with('transactions')->get(); - Then the results are:
$user->transactions;
Edit
if 1 Transaction has 1 TransactionDetail, then this table is not needed I think. Simply add the extra fields to the transactions table.
For Transaction Details, you can then have:
-
transaction_detailstable => id | transaction_id | ... Transaction Model:
public function transactionDetails() { return $this->hasMany(TransactionDetail::class); }
TransactionDetail Model:
public function transaction() { return $this->belongsTo(Transaction::class); }
Then the one you had before becomes:
- Eager Load Relation:
$user = App\User::find(1)->with('transactions.transactionDetails')->get(); - Then the results are:
$user->transactions;To get the details:
@foreach($user->transactions as $transaction)
{{ $transaction->transactionDetails->id; }}
@endforeach