douna4762 2015-07-10 20:14
浏览 127

表与hasManyThrough之间的关系

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

purchases and payments models

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 from purchases inner join payments on payments.id = purchases.payment_id where payments.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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。