Reputation: 637
I have 3 tables
Users table
Loan Amounts Table
Grant Loan amounts table
on my user model i used hasManyThrough relationship
public function loan_amounts()
{
return $this->hasManyThrough(
'App\LoanAmount',
'App\GrantLoanAmount',
'user_id',
'id',
'id',
'amount_id'
);
}
this relationship display the loan_amounts->amount in my user.show blade.
so if i just do {{$user}} it display the following:
{
"id":13,
"name":"antonitte",
"email":"[email protected]",
"email_verified_at":null,
"phone_number":"09472315875",
"verification_code":null,
"phone_verified_at":null,
"avatar":"user.jpg",
"created_at":"2019-09-23 18:50:57",
"updated_at":"2019-09-23 18:50:57",
"loan_amounts":[
{"id":1,"amount":"5000.00","default":1,"created_at":"2019-09-23 02:45:30","updated_at":"2019-09-23 02:45:30","laravel_through_key":13},
{"id":4,"amount":"20000.00","default":0,"created_at":"2019-09-23 02:46:37","updated_at":"2019-09-23 02:46:37","laravel_through_key":13},
{"id":2,"amount":"10000.00","default":0,"created_at":"2019-09-23 02:46:08","updated_at":"2019-09-23 02:46:08","laravel_through_key":13}
]
}
which is good. But my problem is I want to grab the id of grant_loan_amounts->id. so that I can send it to GrantLoanAmountsController@destroy for deletion
any suggestions? thanks!
Upvotes: 1
Views: 621
Reputation: 41
Can try using the eloquent relationships which can be found in the laravel document querying-relationships (check on which laravel version you are using).
From your example you can try something like the following:
this should return a collection
$user->loan_amounts->pluck('id');
this should help to turn into an array
$user->loan_amounts->pluck('id')->toArray();
Upvotes: 1