index 用到了 with (['items.product', 'items.productSku']) items 字段是在哪定义的

 public function index(Request $request)
    {

        $orders = Order::query()
            // 使用 with 方法预加载,避免N + 1问题
            ->with(['items.product', 'items.productSku'])
            ->where('user_id', $request->user()->id)
            ->orderBy('created_at', 'desc')
            ->paginate();

        return view('orders.index', ['orders' => $orders]);
    }
with(['items.product', 'items.productSku']) items字段是在哪定义的,这个地方为什么会用到with懒加载
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
leo
最佳答案

items 是关系,不是字段,也就是我们在 Order 模型中定义的 items() 方法。

4年前 评论
讨论数量: 2
leo

items 是关系,不是字段,也就是我们在 Order 模型中定义的 items() 方法。

4年前 评论

其中 .product.productSKU 应该是模型OrderItem::class中的两个方法。请问大佬我这么理解的对吗? @leo

2年前 评论
leo 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!