我怎么能在null上避免这个错误getClientOriginalExtension()

时间:2018-02-20 14:11:43

标签: laravel

错误

Symfony \ Component \ Debug \ Exception \ FatalThrowableError抛出消息"调用成员函数getClientOriginalExtension()on null"

堆栈跟踪:

0 MYmATH \ app \ Http \ Controllers \ PostsController.php中的Symfony \ Component \ Debug \ Exception \ FatalThrowableError:66

控制器

public function store(Request $request)
  {

    $this->validate(request(),[

        'title' => 'required',

        'image' => 'image|mimes:jpeg,png,jpg,gif,svg',

        'body' => 'required',

    ]);

    $imageName = time().'.'.$request->image_file->getClientOriginalExtension();
    $request->image->move(public_path('image'), $imageName);

    auth()->user()->publish(

        new Post(request(['title','body']))
    );


    session()->flash('message', 'your post has now been published');


    return redirect('/');
}

1 个答案:

答案 0 :(得分:0)

您似乎应该使用image,因为您正在验证image而不是image_file

$request->image->getClientOriginalExtension()