@@ -14,9 +14,21 @@ class PostController extends Controller
14
14
*/
15
15
public function index ()
16
16
{
17
- $ posts = Post::all ();
18
-
19
- return response ()->json ($ posts );
17
+ $ posts = Post::all ()->load ('user ' );
18
+ $ result = [];
19
+ foreach ($ posts as $ post ) {
20
+ array_push ($ result , [
21
+ 'id ' => $ post ->id ,
22
+ 'user_id ' => $ post ->user_id ,
23
+ 'user_name ' => $ post ->user ->name ,
24
+ 'title ' => $ post ->title ,
25
+ 'description ' => $ post ->description ,
26
+ 'created_at ' => $ post ->created_at ,
27
+ 'updated_at ' => $ post ->updated_at ,
28
+ ]);
29
+ }
30
+
31
+ return response ()->json ($ result );
20
32
}
21
33
22
34
/**
@@ -37,12 +49,13 @@ public function create()
37
49
*/
38
50
public function store (Request $ request )
39
51
{
40
- $ post = $ request ->user ()->posts ()->create ([
52
+ $ post = Post::create ([
53
+ 'user_id ' => $ request ->user_id ,
41
54
'title ' => $ request ->title ,
42
55
'description ' => $ request ->description
43
56
]);;
44
57
45
- return response ()->json ($ post );
58
+ return response ()->json ($ post-> load ( ' user ' ) );
46
59
}
47
60
48
61
/**
@@ -77,6 +90,7 @@ public function edit($id)
77
90
public function update (Request $ request , $ id )
78
91
{
79
92
$ post = Post::find ($ id )->update ([
93
+ 'user_id ' => $ request ->user_id ,
80
94
'title ' => $ request ->title ,
81
95
'description ' => $ request ->description
82
96
]);;
0 commit comments