duanjiao3686 2018-08-10 09:34
浏览 63
已采纳

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

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'));
    }
}
  • 写回答

1条回答 默认 最新

  • doutuosai3504 2018-08-10 10:42
    关注

    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'));
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?