3

I use query builder for execution inquiries to db like as:

$result = Order::with("product", "images");

As result I get response with two nested objects inside:

{["product" : [{}], "images" : [{}]]}

How can I join these two objects to one, that to get the following response:

{"title" : "Product name", "price" : 3, "images" :{}, "order" : {} }
2
  • what is the relation between Order and product, images? Also, are you sure there is a relation between order and images and not product and images? Commented Jan 2, 2017 at 9:04
  • Relations between Order and Product is: Order.product_id = Product.id and Product.image = images.id Commented Jan 2, 2017 at 9:10

1 Answer 1

2

You should use nested eager loading here:

Order::with('products.images')->first();

If you want to get an array or JSON instead of collection, use toArray() or toJson() methods on collection.

Sign up to request clarification or add additional context in comments.

3 Comments

You have to remove the first products, because products.images will load products.
In this case you will get object products and inside object images. But I need join this in one object product
@Hamama did you try to use toArray() or toJson() as I suggested?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.