Illuminate\Validation\Factory::make() 必须是数组类型,object 给定

Illuminate\Validation\Factory::make() must be of the type array, object given

框架:Laravel 5.4

问题:

上传文件时出现标题错误。 但是如果我不上传文件,验证就可以了。 $request我也查了,还是不知道是什么问题

这是我的 $request 截图 $request

这是我的代码

$rules = [
          'edu_title'  => 'max:255',
          'edu_author' => 'max:10',
          'categories' => 'max:100',
          'file_name'  => 'nullable|file',
          'updated_at' => 'date|before:tomorrow'
         ];

    $messages = [
                'required' => '此欄位必填',
                'max'      => '此欄位最大長度必須小於 :max',
                'date'     => '請選擇日期',
                'before'   => '選擇的日期錯誤'
                ];

    $validate = Validator::make($request->all(), $rules, $messages);

    if($validate->fails()) {
        return back()->witherrors($validate)->withInput();
    }

    $path = '';

    if($request->hasFile('file_name')) {
        
    $validate = Validator::make($request->file_name, ['file_name' => 'file']);
        
    if( Storage::disk('local')->exists($directories.'/'.$file_name) ) {
            return back()->with('file exists', '檔案已經存在或檔名重複')->withInput();
        }

        $path = $request->file('file_name')->storeAs(
                $directories, $file_name);             
    }
    
    OtherTeachingMaterial::find($id)->update([
                            'edu_title'  => $request->edu_title,
                            'edu_author' => $request->edu_author,
                            'file_path'  => $path,
                            'categories' => $request->categories,
                            'updated_at' => $request->updated_at
                        ]);

    return redirect()->route('others_teahcing_material.dashboard')->with('success', '更新成功');

您可以添加您的文件rule到您的$rules数组中并删除您的第二个Validate::make() 方法,所以你的 $rules 数组将是:

$rules = [
          'edu_title'  => 'max:255',
          'edu_author' => 'max:10',
          'categories' => 'max:100',
          'file_name'  => 'nullable|file',
          'updated_at' => 'date|before:tomorrow',
          'file_name' => 'file'
         ];