Return Json with related data - Laravel

2

I'm creating a REST API but I stumbled on this issue where I can not imagine a solution.

In my database I have 2 tables;

Table 1 - General product data such as name, code, sku and description

Table 2 - Variable product codes.

Table 3 - Product Images

For example; Product X contains sizes 1,2,3 and 2 images (these sizes and images are in separate tables of table 1).

How would I return a json with the related data between tables? Currently I was only able to return a " all " from table 1, what would be the logic applied in that situation so that I would return a json with product 1 and variations thereof?

    
asked by anonymous 30.11.2015 / 13:08

1 answer

1

I was able to find the solution in this topic.

After creating the relationship in both controllers, models and migrations;

Basically what should be done to fetch related data is:

$dados = Post::with('Comments')->get();
return response()->json($dados);
  

Considering the oneToMany relationship

    
30.11.2015 / 14:49