5
5
use App \Http \Controllers \Controller ;
6
6
use Illuminate \Http \Request ;
7
7
use App \Models \Post ;
8
+ use App \Models \User ;
8
9
use Illuminate \Support \Facades \Auth ;
9
10
use Illuminate \Support \Facades \Validator ;
10
11
use App \Http \Requests \PostRequest ;
@@ -15,25 +16,45 @@ class PostController extends Controller
15
16
{
16
17
use ApiTrait, Media;
17
18
18
- public function getPosts ()
19
- {
20
- $ posts = Post::orderBy ('created_at ' , 'desc ' )->get ();
21
- return $ this ->data (compact ('posts ' ));
22
- }
19
+ public function getPosts ()
20
+ {
21
+ // Retrieve all posts with the user relationship and order them by 'created_at' in descending order
22
+ $ posts = Post::with ('user ' )->orderBy ('created_at ' , 'desc ' )->get ();
23
+
24
+ // Transform the posts data to include user_name and user_imageUrl
25
+ $ postData = $ posts ->map (function ($ post ) {
26
+ $ data = $ post ->toArray ();
27
+ $ data ['user_name ' ] = $ post ->user ->name ; // Change 'name' to the actual column name in your users table
28
+ $ data ['user_imageUrl ' ] = $ post ->user ->imageUrl ; // Change 'imageUrl' to the actual column name in your users table
29
+ unset($ data ['user ' ]); // Remove the user relationship to avoid redundancy
30
+ return $ data ;
31
+ });
32
+
33
+ return $ this ->data (compact ('postData ' ));
34
+ }
35
+
23
36
24
37
public function showPost ($ id )
25
38
{
26
- $ post = Post::find ($ id );
27
-
39
+ $ post = Post::with ( ' user ' )-> find ($ id );
40
+
28
41
if (!$ post ) {
29
42
return $ this ->errorMessage ([], 'Post not found ' , 404 );
30
43
}
31
-
44
+
45
+ // Convert the Post model and user model to an array
32
46
$ postData = $ post ->toArray ();
33
-
47
+
48
+ // Replace the user_id with user's name and imageUrl
49
+ $ postData ['user_name ' ] = $ post ->user ->name ; // Change 'name' to the actual column name in your users table
50
+ $ postData ['user_imageUrl ' ] = $ post ->user ->imageUrl ; // Change 'imageUrl' to the actual column name in your users table
51
+
52
+ // Unset the user relationship to avoid redundancy
53
+ unset($ postData ['user ' ]);
54
+
34
55
return $ this ->data ($ postData , 'Post retrieved successfully ' , 200 );
35
56
}
36
-
57
+
37
58
38
59
public function createPost (PostRequest $ request )
39
60
{
@@ -51,12 +72,12 @@ public function createPost(PostRequest $request)
51
72
$ post ->content = $ request ->input ('content ' );
52
73
}
53
74
54
- // Handle image upload, if provided
55
- if ($ request ->hasFile ('file_path ' )) {
75
+ // Handle image upload, if provided
76
+ if ($ request ->hasFile ('file_path ' )) {
56
77
$ image = $ request ->file ('file_path ' );
57
78
$ imagePath = $ this ->upload ($ image , 'posts ' );
58
79
$ post ->image_path = "images/posts/ $ imagePath " ;
59
- }
80
+ }
60
81
61
82
62
83
$ post ->save ();
@@ -85,11 +106,11 @@ public function editPost(PostRequest $request, $id)
85
106
}
86
107
87
108
// Handle image upload, if provided
88
- if ($ request ->hasFile ('file_path ' )) {
109
+ if ($ request ->hasFile ('file_path ' )) {
89
110
$ image = $ request ->file ('file_path ' );
90
111
$ imagePath = $ this ->upload ($ image , 'posts ' );
91
112
$ post ->image_path = "images/posts/ $ imagePath " ;
92
- }
113
+ }
93
114
94
115
$ post ->save ();
95
116
0 commit comments