douliang9057 2014-10-11 13:03
浏览 29

在变形模型上的急切负载关系

Say I have a model that morphs a number of different models (using morphTo). I'm trying to achieve that whenever a model is morphed, all (or some) relations on that morphed model are eager loaded too.

Example code here: http://laravel.io/bin/yLyz7

Whenever I'd call:

Index::with('item')->get();

It will eventually result in a query like so for the morph:

SELECT * FROM sample_items WHERE id IN (?, ?, ?)

This is exactly what I want, but I also want to eager load any relations on the SampleItem models, so that instead of querying the relation for each SampleItem every time, it would also create a query like this. Assuming SampleItem has a "user" relation.

SELECT * FROM users WHERE id IN (?, ?, ?)

Is there any way to achieve this? Because I'm not calling the SampleItem model myself, I can't specify any eager loaded relations. I assume I'd have to specify this in the relation on Index, but I can't figure out how. I'm not sure this is even currently possible.

  • 写回答

1条回答 默认 最新

  • duanguoyin7008 2014-10-11 13:28
    关注

    Turns out to be easier than I thought. Seems like specifying the "with" property on the SampleItem model takes care of this:

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    protected $with = ['user'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里