Sep 29, 2017
0
Level 2
How to return datas as array for child components with relationship as JSON in lumen 5.5
I have a basic relationship for posts, which has relationship with its user and its category. When i return it with json i only get array for the Parent component (i.e) only for the posts. My user datas are only coming as a instance. ResultingI cannot grab it in javascript. Here is my function and response,
public function index()
{
$posts = Post::with('user', 'category')->get();
return response()->json($posts);
}
My response is
[
{
"id": 1,
"user_id": 7,
"title": "tis is title",
"description": "Occaecati ad ipsum esse alias officiis exercitationem. Nihil dolor autem sed id fugiat eveniet numquam. In ipsum aliquam quod.",
"category_id": "4",
"slug": "this-is-title",
"created_at": "2017-09-24 11:40:55",
"updated_at": "2017-09-25 00:18:29",
"user": {
"id": 7,
"username": "lucious.greenholt",
"name": "7",
"email": "gregorio37@bailey.com",
},
"category": {
"id": 4,
"name": "ashly.farrell",
"created_at": "2017-09-24 11:40:56",
"updated_at": "2017-09-24 11:40:56"
}
}
]
See the user and category not coming as an array. Can someone help?
Please or to participate in this conversation.