简体   繁体   English

Laravel 上传照片可选

[英]Laravel upload photo optional

    public function submitReview(Request $request){
        
        $request->validate([
            'comment'=> 'required',
            'R_Image' => 'mimes:kpg,png,jpeg|max:5048'


        ]);

        
            
        $newImageName = time() . '-' . $request->name . '.' . 
        $request->R_Image->extension();
        $request->R_Image->move(public_path('images'), $newImageName);
    


        $UserId=Auth::id();
        $query = DB::table('review') ->insert ([

                        'User_Id'=> $UserId,
                        'P_Id'=>$request->input('productID' ),
                        'R_Rating'=>$request->input('R_Rating' ),
                        'R_Comment'=>$request->input('comment' ),
                        'R_Image'=>$newImageName,
                        "created_at" =>  \Carbon\Carbon::now(), # new \Datetime()
                        "updated_at" => \Carbon\Carbon::now(), 
                    

                    ]);
        
        if ($query) {
            return back()-> with ('success' , 'Review has been successfully submitted');
        }else{
            return back() -> with ('fail' , 'Something went wrong');
        }

    }
}

This is my code for review.这是我的审查代码。 The user can upload image upon submitting the form and the image is not required which is optional.用户可以在提交表单时上传图片,图片不是必需的,这是可选的。 However when the user do not upload image I get the error saying "Call to a member function extension() on null".但是,当用户不上传图像时,我收到错误消息“呼叫成员 function extension() on null”。 But if submit the form with the image, i got no error.但是,如果提交带有图像的表单,我没有收到错误。 Is there something wrong with my code?我的代码有问题吗?

'R_Image' => 'mimes:`jpg`,png,jpeg|max:5048' 
public function submitReview(Request $request){
        
        $request->validate([
            'comment'=> 'required'
            ]);

        $newImageName = "";

        if ($request->hasFile('R_Image')) {
            $request->validate([
            'R_Image' => 'mimes:kpg,png,jpeg|max:5048'
            ]);

            $newImageName = time() . '-' . $request->name . '.' . 
            $request->R_Image->extension();
            $request->R_Image->move(public_path('images'), $newImageName);
        }

        $UserId=Auth::id();
        $query = DB::table('review') ->insert ([

                        'User_Id'=> $UserId,
                        'P_Id'=>$request->input('productID' ),
                        'R_Rating'=>$request->input('R_Rating' ),
                        'R_Comment'=>$request->input('comment' ),
                        'R_Image'=>$newImageName,
                        "created_at" =>  \Carbon\Carbon::now(), # new \Datetime()
                        "updated_at" => \Carbon\Carbon::now(), 
                    

                    ]);
        
        if ($query) {
            return back()-> with ('success' , 'Review has been successfully submitted');
        }else{
            return back() -> with ('fail' , 'Something went wrong');
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM