SleepWalker
SleepWalker

Reputation: 637

Eloquent relation for 3 tables

I'd like to display the loans granted to a certain user specifically on user.show.

here's the table for loan amounts enter image description here

here's the Grant loan table

enter image description here

now on user.show i'd like to display the loans granted to that user

on my loan amount model,

public function grantLoans()
{
    return $this->manyToMany('App\LoanAmount','id','amount_id');
}    

on my GrantLoanAmount Model

public function user() //just update this. this line works
{
    return $this->hasMany('App\User','id','user_id');
}

public function amounts() //just update this. this line works
{
    return $this->hasMany('App\LoanAmount','id','amount_id');
}

on my userController

currently I only have this,

public function show($id)
{
$grantLoan = GrantLoanAmount::where('user_id',$id)->get();
?????
}

how can I the amount? form grant_loan_amounts table?

thank you!

Upvotes: 2

Views: 64

Answers (1)

lkdhruw
lkdhruw

Reputation: 582

Assuming you have a User model, a LoanAmount model and a GrantLoanAmount model and a User can be granted more than one loan amount then this should work

//User model
public function loan_amounts()
{
    return $this->hasManyThrough(
        'App\LoanAmount',
        'App\GrantLoanAmount',
        'user_id',
        'id',
        'id',
        'amount_id'
    );
}

Upvotes: 1

Related Questions