【问题标题】:Laravel upload photo optionalLaravel 上传照片可选
【发布时间】:2022-01-17 07:43:02
【问题描述】:
    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');
        }

    }
}

这是我的审查代码。用户可以在提交表单时上传图片,图片不是必需的,这是可选的。但是,当用户不上传图片时,我收到错误消息“调用 null 时的成员函数 extension()”。但是,如果提交带有图像的表单,我没有收到错误。我的代码有问题吗?

【问题讨论】:

标签: laravel file upload


【解决方案1】:
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');
        }

    }
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

'R_Image' => 'mimes:jpg,png,jpeg|max:5048' mime 类型必须为 jpg。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 2023-03-12
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多