I got 3 models in Laravel (User, Payment, Purchase).
What I want is pretty simple but i've no idea how to do it with Laravel/Eloquent ; I would succeed with raw SQL.
I want to get the purchases of a user without much code.
The users
table is linked with the payments
table which is linked to the purchases
table. The finale code would look like
$purchases = $user->purchases()->get();
My models look like this
So I tried a simple
public function purchases()
{
return $this->hasManyThrough('Purchase', 'Payment');
}
But it doesn't work
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'purchases.payment_id' in 'on clause' (SQL: select
purchases
.*,payments
.user_id
frompurchases
inner joinpayments
onpayments
.id
=purchases
.payment_id
wherepayments
.user_id
= 1)
Pretty simple here, it searches purchases
.payment_id
but my table organization needs a join with payment
.purchase_id
. How can you tell that to Laravel ? Is there another way to do what I want ?
Maybe I could do it in raw SQL but keep the object generated via Eloquent ? I'm open to any solution ^^
Detail : I cannot rewrite my tables.