因为之前注册了resource路由 所以我们可以直接在PostsController的edit方法中实现逻辑:
public function edit($id) { // 通过id取到文章模型 $discussion = Discussion::findOrFail($id); // 返回视图 return view(‘forum.edit‘, compact(‘discussion‘)); }
创建edit.blade.php (我们使用了form的模型绑定):
@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方法:
public function update(Requests\StorePostsRequest $request, $id) { // 取到数据 $discussion = Discussion::findOrFail($id); $discussion->update($request->all()); return redirect()->action(‘[email protected]‘, [‘id‘ => $discussion->id]); }
时间: 2024-11-08 21:19:56