【问题标题】:Laravel: a model that belongs to two other modelsLaravel:属于其他两个模型的模型
【发布时间】:2016-04-09 22:04:34
【问题描述】:

在laravel中,有没有办法获取属于两个模型的对象列表?

例如,

模型交易: 属于用户 所属类别

模型用户: 有很多交易

型号类别: 有很多交易

假设这些关系被正确定义,你会做什么样的查询,比如控制器,以访问属于用户 x 和类别 y 的交易集?

【问题讨论】:

  • 告诉我们你到目前为止做了什么

标签: php laravel laravel-5 eloquent laravel-5.1


【解决方案1】:

假设您的Transaction 表具有user_idcategory_id 的外键,您可以这样做:

$transactions = Transaction::where('user_id', '=', 'x')
                          ->where('category_id ', '=', 'y')
                          ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多