I have this controller method to add a course and it works fine but I really don't like this code because I think it's too large and a little bit ugly
I was trying to do it cleaner and a little bit shorter, But don't have knowledge to do that so can anyone please help me with this code?
public function storeCourse(Request $request)
{
$request->validate([
'slug' => 'required',
'name' => 'required',
'image' => 'required',
'description' => 'required',
'body' => 'required',
'audio' => 'required',
'price' => 'required',
'level' => 'required',
'subtitle' => 'required',
'knowledge' => 'required',
'required' => 'required',
'userId' => 'required',
'categoryId' => 'required'
]);
if ($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
$request->image->move(public_path('/uploads'), $fileNameToStore);
} else {
$fileNameToStore = 'no_img';
}
$course = new Course();
$course->slug = $request->get('slug');
$course->name = $request->get('name');
$course->image = $fileNameToStore;
$course->description = $request->get('description');
$course->body = $request->get('body');
$course->audio = $request->get('audio');
$course->price = $request->get('price');
$course->level = $request->get('level');
$course->subtitle = $request->get('subtitle');
$course->knowledge = $request->get('knowledge');
$course->required = $request->get('required');
$course->user_id = $request->get('userId');
$course->course_category_id = $request->get('categoryId');
$course->save();
return redirect()->back();
}