在许多输入中上传文件-Laravel

时间:2018-08-10 09:34:53

标签: php laravel file-upload controller laravel-5.6

我有一个表单,试图从两个不同的输入上载两个文件,但是我的控制器有问题,我不知道如何编写存储和更新功能!!?

  

我的商店功能可以正常工作,但我不确定它是否正确。


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'));
    }
}

1 个答案:

答案 0 :(得分:0)

SingerController:

public function update(Request $request, Singer $singer)

    {
        $imageUrl = $request->file(['singer_image' , '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'));
    }