I have a form that I'm trying to upload two files from two different inputs but I have problems in my controller and I don't know how to write store & update function !!!?
My store Function it works but I'm not sure it's correct.
AdminController.php :
class AdminController extends Controller
{
protected function singerUploadImage($file)
{
$year = Carbon::now()->year;
$filename = date('Y_m_d') . '_' . $file->getClientOriginalName();
$destination = "/uploads/images/{$year}/singers";
$destinationPath = public_path($destination);
$file->move($destinationPath, $filename);
return $destination . '/' . $filename;
}
}
SingerController.php :
class SingerController extends AdminController
{
/**
* Store a newly created resource in storage.
*
* @param SingerRequest|Request $request
* @return \Illuminate\Http\Response
*/
public function store(SingerRequest $request)
{
$imageUrl['singer_image'] = $this->singerUploadImage($request->file('singer_image'));
$imageUrl['singer_bg'] = $this->singerUploadImage($request->file('singer_bg'));
Singer::create(array_merge($request->all() , ['singer_image' => $imageUrl['singer_image'] , 'singer_bg' => $imageUrl['singer_bg']]));
return redirect(route('singers.index'));
// return $request->all();
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Singer $singer
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Singer $singer)
{
$imageUrl['singer_image'] = $this->singerUploadImage($request->file('singer_image'));
$imageUrl['singer_bg'] = $this->singerUploadImage($request->file('singer_bg'));
$input = $request->all();
if ($request->hasFile($imageUrl['singer_image'])){
$input ['singer_image'] = $this->singerUploadImage($request->file('singer_image'));
}else{
$input ['singer_image'] = $singer->singer_image;
}
if($request->hasFile($imageUrl['singer_bg'])){
$input ['singer_bg'] = $this->singerUploadImage($request->file('singer_bg'));
}else{
$input ['singer_bg'] = $singer->singer_bg;
}
$singer->update($input);
return redirect(route('singers.index'));
}
}