我如何使用Store()方法在laravel 5.2中的数据库中存储图像

时间:2017-04-11 14:54:28

标签: laravel laravel-5.2 store

当我填写提交表单时,它会在Macroable.php第74行中给出这个按钮BadMethodCallException: 方法存储不存在。

ArticleController

public function store(Request $request)
{
    $file = $request->file('attach');
    $filename = $file->store('local');
    $article = new Article;
    $article->title = $request->title;
    $article->body = $request->body;
    $article->attachment = $filename;
    $article->save();
    Session::flash('msg','Your data is saved now');
    return back();
}

addarticle.blade.php

{{Session::get('msg')}}
<form class="container col-lg-6" action="article" method="post" enctype ="multipart/form-data">
{{csrf_field()}}
<div class="form-group"></div>
    Title <input type="text"  class="form-control" name="title">
    Body <textarea name="body" id="" class="form-control" cols="30" rows="10">
</textarea>
    <input type="file" name="attach">
    <input type="submit">
</div>

路线

 Route::get('/', function () {
 return view('welcome');
 });
 Route::get('article','ArticleController@index');
 Route::post('article','ArticleController@store');
 Route::get('allarticle','ArticleController@show');

2 个答案:

答案 0 :(得分:1)

将您的初始表单标记行更改为:

<form class="container col-lg-6" action="{{action('ArticleController@store')}}" method="post" enctype="multipart/form-data">

这将生成一个提交表单的URL,并且不会出错。因为错误可能是您已经在/article并且action="article"生成了像/article/article这样的网址。

PS:根据&#34; bootstrap的标准,您的HTML无效。

答案 1 :(得分:0)

更改您的表单标记,如下所示: -

<form class="container col-lg-6" action="{{ url('article') }}" method="post" enctype ="multipart/form-data">{{csrf_field()}}