Skip to content

Commit a0f3a70

Browse files
Edit image_path
1 parent d9c0904 commit a0f3a70

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

app/Http/Controllers/Api/PostController.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ public function createPost(PostRequest $request)
5050
if ($request->filled('content')) {
5151
$post->content = $request->input('content');
5252
}
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+
6061

6162
$post->save();
6263
return $this->successMessage('Post created successfully', 201);
@@ -69,11 +70,6 @@ public function editPost(PostRequest $request, $id)
6970
return $this->errorMessage([], 'Post not found', 404);
7071
}
7172

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-
7773
if ($post->user_id !== Auth::id()) {
7874
return $this->errorMessage([], 'You are not authorized to edit this post', 403);
7975
}
@@ -88,13 +84,13 @@ public function editPost(PostRequest $request, $id)
8884
$post->content = $request->input('content');
8985
}
9086

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+
9894
$post->save();
9995

10096
if ($request->filled('content') || $request->hasFile('file_path')) {

app/Http/Requests/PostRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function rules()
2525
{
2626
return [
2727
'content' => 'string|max:1000',
28-
'file_path' => 'image|mimes:jpeg,png,jpg,gif|max:2048', // Adjust the allowed file types and maximum size as needed.
28+
'file_path' => 'image|mimes:jpeg,png,jpg,gif|max:4000', // Adjust the allowed file types and maximum size as needed.
2929
];
3030
}
3131

app/Traits/Media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ trait Media
55
{
66
public static function upload($image,string $dir) :string
77
{
8-
$photoName = 'images/'.uniqid() . '.' . $image->extension();
8+
$photoName = uniqid() . '.' . $image->extension();
99
$image->move(public_path("images/$dir"),$photoName);
1010
return $photoName;
1111
}

0 commit comments

Comments
 (0)