如何加入 Laravel 中 with 运算符的结果?
How to join results from with operator in Laravel?
我使用查询构建器来执行对数据库的查询,如下所示:
$result = Order::with("product", "images");
结果我得到了包含两个嵌套对象的响应:
{["product" : [{}], "images" : [{}]]}
如何将这两个对象合并为一个对象,以获得以下响应:
{"title" : "Product name", "price" : 3, "images" :{}, "order" : {} }
你应该在这里使用 nested eager loading:
Order::with('products.images')->first();
如果您想获取数组或 JSON 而不是集合,请对集合使用 toArray()
或 toJson()
方法。
我使用查询构建器来执行对数据库的查询,如下所示:
$result = Order::with("product", "images");
结果我得到了包含两个嵌套对象的响应:
{["product" : [{}], "images" : [{}]]}
如何将这两个对象合并为一个对象,以获得以下响应:
{"title" : "Product name", "price" : 3, "images" :{}, "order" : {} }
你应该在这里使用 nested eager loading:
Order::with('products.images')->first();
如果您想获取数组或 JSON 而不是集合,请对集合使用 toArray()
或 toJson()
方法。