Home > Software engineering >  Laravel upload photo optional
Laravel upload photo optional

Time:01-18

    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?

CodePudding user response:

'R_Image' => 'mimes:`jpg`,png,jpeg|max:5048' 

CodePudding user response:

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

    }
}
  •  Tags:  
  • Related