@@ -50,13 +50,14 @@ public function createPost(PostRequest $request)
50
50
if ($ request ->filled ('content ' )) {
51
51
$ post ->content = $ request ->input ('content ' );
52
52
}
53
-
54
- // Handle image upload, if provided
55
- if ($ request ->hasFile ('file_path ' )) {
56
- $ image = $ request ->file ('file_path ' );
57
- $ imagePath = $ this ->upload ($ image , 'posts ' );
58
- $ post ->image_path = $ imagePath ;
59
- }
53
+
54
+ // Handle image upload, if provided
55
+ if ($ request ->hasFile ('file_path ' )) {
56
+ $ image = $ request ->file ('file_path ' );
57
+ $ imagePath = $ this ->upload ($ image , 'posts ' );
58
+ $ post ->image_path = "images/posts/ $ imagePath " ;
59
+ }
60
+
60
61
61
62
$ post ->save ();
62
63
return $ this ->successMessage ('Post created successfully ' , 201 );
@@ -69,11 +70,6 @@ public function editPost(PostRequest $request, $id)
69
70
return $ this ->errorMessage ([], 'Post not found ' , 404 );
70
71
}
71
72
72
- // Check if both 'content' and 'file_path' are provided
73
- if ($ request ->filled ('content ' ) && $ request ->hasFile ('file_path ' )) {
74
- return $ this ->errorMessage ([], 'Either content or an image can be updated, but not both ' , 422 );
75
- }
76
-
77
73
if ($ post ->user_id !== Auth::id ()) {
78
74
return $ this ->errorMessage ([], 'You are not authorized to edit this post ' , 403 );
79
75
}
@@ -88,13 +84,13 @@ public function editPost(PostRequest $request, $id)
88
84
$ post ->content = $ request ->input ('content ' );
89
85
}
90
86
91
- // Handle image update , if provided
92
- if ($ request ->hasFile ('file_path ' )) {
93
- $ image = $ request ->file ('file_path ' );
94
- $ imagePath = $ this ->upload ($ image , 'posts ' );
95
- $ post ->image_path = $ imagePath;
96
- }
97
-
87
+ // Handle image upload , if provided
88
+ if ($ request ->hasFile ('file_path ' )) {
89
+ $ image = $ request ->file ('file_path ' );
90
+ $ imagePath = $ this ->upload ($ image , 'posts ' );
91
+ $ post ->image_path = " images/posts/ $ imagePath" ;
92
+ }
93
+
98
94
$ post ->save ();
99
95
100
96
if ($ request ->filled ('content ' ) || $ request ->hasFile ('file_path ' )) {
0 commit comments