我按照laravel5.1的教程做了博客的增删改,发现一个问题一直没找到原因,就是在修改的时候,对同一条记录不停的修改两三次,前几次在点击submit的时候一切正常,最后一次点击submit,就会出现网页一直载入的状态,过一会儿就会报下面的错误,看这个错误是不是说除了POST方法,其他方法都不允许,可是修改记录应该是PATCH啊,
(1/1) MethodNotAllowedHttpException
in RouteCollection.php (line 251)
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE'))
in RouteCollection.php (line 238)
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE'))
in RouteCollection.php (line 176)
at RouteCollection->match(object(Request))
in Router.php (line 546)
at Router->findRoute(object(Request))
in Router.php (line 525)
我用的是5.4版本,控制器是用
php artisan make:controller ArticleController --resource
生成的,模型和视图基本和视频里的保持一致。
下面是路由相关的设置,这些都是自动生成的:
PUT|PATCH | articles/{article} | articles.update
GET|HEAD | articles/{article}/edit | articles.edit
还有一个现象是,当页面出现一直载入的状态时,这时候再次点击submit,就能正常提交。
控制器:
public function edit(Article $article) {
return view('articles.edit', compact('article'));
}
public function update(Request $request, $id) {
$article = Article::findOrFail($id);
$article->update($request->all());
return redirect('/articles');
}
视图:
{!! Form::model($article, ['url' => 'articles/'.$article->id, 'method' => 'PATCH']) !!}
@include('articles.form')
{!! Form::submit('编辑文章', ['class' => 'btn btn-primary form-control']) !!}
{!! Form::close() !!}
模型:
class Article extends Model
{
protected $fillable = ['user_id', 'title', 'content', 'intro', 'published_at'];
protected $dates = ['published_at'];
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
public function scopePublished($query)
{
return $query->where('published_at', '<=', Carbon::now());
}
public function user()
{
return $this->belongsTo('App\User');
}
}
大神们,帮帮我~~~