简体   繁体   中英

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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