【问题标题】:Eager Loading in Laravel EloquentLaravel Eloquent 中的急切加载
【发布时间】:2021-10-02 22:25:58
【问题描述】:

我是 laravel 的新手。例如,我想问一下我有 report_type 模型和报告模型,这是一对多的关系。我想显示 report_types 以及报告。这个的最佳实践是什么?我应该只使用 report.report_type 还是我还必须在急切加载中添加报告。

$report_histories = Report_history::where('report_id', $report->id)->with(['employee', 'report', 'report.report_type'])->get();

【问题讨论】:

  • 你必须写每一个你想加载的东西,所以如果你想加载report和report_type,你必须像你一样写。

标签: laravel eager-loading


【解决方案1】:
$report_histories = Report_history::with('report.report_type','employee')
                                    ->where('report_id', $report->id)
                                    ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-29
    • 2013-06-10
    • 2019-07-08
    • 1970-01-01
    • 2017-09-30
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多