无法在laravel 5.4中上传图片

时间:2018-12-27 16:36:43

标签: laravel

嗨,我正在尝试将图像上传到数据库中,而这一切都给了我这样的错误。

  

(1/1)BadMethodCallException   方法getClientOrignalName不存在。

<form action="{{route('post.store')}}" method="post" enctype="multipart/form-data">**strong text**

公共功能存储(请求$ request)     {

    $this->validate($request,[


     'title'  => 'required|max:255',
     'content' => 'required',
     'feature' => 'required|image',
     'category_id' => 'required' 



    ]);
        // dd($request->all());
         //exit;

    $featured = $request->feature;
    $featured_new_name=time().$featured->getClientOrignalName();

    $featured->move('uploads/posts',$featured_new_name);

    $post = Post::create([

    'title'=>$request->title,
    'content'=>$request->content,
    'feature'=>'uploads/posts/'. $featured_new_name,
    'category_id'=>$request->category_id

    ]);

    Session::flash('success','Post Created Successfully.');


}

1 个答案:

答案 0 :(得分:0)

您应该使用file()方法从请求中检索文件信息。尝试此代码,

public function store(Request $request) {

    $this->validate($request,[
        'title'  => 'required|max:255',
        'content' => 'required',
        'feature' => 'required|image',
        'category_id' => 'required' 

    ]);

    // use file() method for retrive file data
    $featured = $request->file('feature');

    $featured_new_name = time() . $featured->getClientOrignalName();

    $featured->move('uploads/posts', $featured_new_name);

    $post = Post::create([
        'title'=>$request->title,
        'content'=>$request->content,
        'feature'=>'uploads/posts/'. $featured_new_name,
        'category_id'=>$request->category_id
    ]);

    Session::flash('success','Post Created Successfully.');
}