由於以前註冊了resource路由 因此咱們能夠直接在PostsController的edit方法中實現邏輯:php
public function edit($id) { // 經過id取到文章模型 $discussion = Discussion::findOrFail($id); // 返回視圖 return view('forum.edit', compact('discussion')); }
建立edit.blade.php (咱們使用了form的模型綁定):app
@extends('app') @section('content') <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2" role="main"> {!! Form::model($discussion,['method'=>'patch','url'=>'/discussions/' . $discussion->id]) !!} @include('forum.form_discussion') <!-- 提交 --> {!! Form::submit('提交',['class' => 'btn btn-primary form-control']) !!} {!! Form::close() !!} <div> @if($errors->any()) <ul class="list-group"> @foreach($errors->all() as $error) <li class="list-group-item list-group-item-danger">{{ $error }}</li> @endforeach </ul> @endif </div> </div> </div> </div> @stop
當點擊了提交按鈕後就會跳轉到PostsController的update方法:url
public function update(Requests\StorePostsRequest $request, $id) { // 取到數據 $discussion = Discussion::findOrFail($id); $discussion->update($request->all()); return redirect()->action('PostsController@show', ['id' => $discussion->id]); }