Please I have an issue with my code. whenever I want to update my post it always says my files are empty although I have an old file. Please can you help me to fix it? Thanks in advance.
This my controller
public function update(Request $request, $id)
{
$allowedOptions = Constants::ACTIVE . ",". Constants::INACTIVE;
$allowedTypes = Constants::MUSIC;
$data = $request->validate([
'category_id' => "required|string",
'name' => 'required|string',
'content_desccription' => 'required:string',
"type" => "required|string|in:$allowedTypes",
"meta_title" => "required|string",
"meta_keywords" => "required|string",
"meta_description" => "required|string",
"is_sponsored" => "required|string|in:$allowedOptions",
"is_top_story" => "required|string|in:$allowedOptions",
"is_featured" => "required|string|in:$allowedOptions",
"is_published" => "required|string|in:$allowedOptions",
"can_comment" => "required|string|in:$allowedOptions",
]);
if (!empty([
$image_path = MediaFilesHelper::saveFromRequest($request->cover_image, "postImages", $request),
$music_path = MediaFilesHelper::saveFromRequest($request->cover_music, "postMusic", $request),
]));
$data['cover_image'] = $image_path;
$data['cover_music'] = $music_path;
$post = Post::where('id', $id)->update($data);
return redirect()->route('admin.post.index')->with('success_meassage', 'Post updated successfully');
}
This is the code handly the file
public static function saveFromRequest($file , $path , Request $request)
{
$file_name = Str::slug($request->name, '-').".".$file->extension();
$file->move(public_path($path) , $file_name);
return $path."/".$file_name;
}
CodePudding user response:
I have changed your file condition now try I hope it work for you now.
public function update(Request $request, $id)
{
$allowedOptions = Constants::ACTIVE . ",". Constants::INACTIVE;
$allowedTypes = Constants::MUSIC;
$data = $request->validate([
'category_id' => "required|string",
'name' => 'required|string',
'content_desccription' => 'required:string',
"type" => "required|string|in:$allowedTypes",
"meta_title" => "required|string",
"meta_keywords" => "required|string",
"meta_description" => "required|string",
"is_sponsored" => "required|string|in:$allowedOptions",
"is_top_story" => "required|string|in:$allowedOptions",
"is_featured" => "required|string|in:$allowedOptions",
"is_published" => "required|string|in:$allowedOptions",
"can_comment" => "required|string|in:$allowedOptions",
]);
if ($request->file('cover_image')) {
$image_path = MediaFilesHelper::saveFromRequest($request->cover_image, "postImages", $request);
$data['cover_image'] = $image_path;
}
if ($request->file('cover_music')) {
$music_path = MediaFilesHelper::saveFromRequest($request->cover_music, "postMusic", $request);
$data['cover_music'] = $music_path;
}
$post = Post::where('id', $id)->update($data);
return redirect()->route('admin.post.index')->with('success_meassage', 'Post updated successfully');
}