多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 第8节 Laravel-表单中查看详情及删除 # 8.1 给HTML添加链接 8.2 实现查看详情 8.3 实现删除 ## 8.1 给HTML添加链接 ## 如下,修改视图文件,添加链接地址 <td> <a href="{{ url('student/detail', ['id' => $student->id]) }}">详情</a> <a href="{{ url('student/update', ['id' => $student->id]) }}">修改</a> <a href="{{ url('student/delete', ['id' => $student->id]) }}">删除</a> </td> 添加路由: Route::any('student/detail/{id}', ['uses' => 'StudentController@detail']); Route::any('student/delete/{id}', ['uses' => 'StudentController@delete']); 创建查看详情页视图 `\resources\views\student\detail.blade.php` ~~~ @extends('layout/student') @section('content') <!-- 自定义内容区域 --> <div class="panel panel-default"> <div class="panel-heading">学生详情</div> <table class="table table-bordered table-striped table-hover "> <tbody> <tr> <td width="50%">ID</td> <td>1</td> </tr> <tr> <td>姓名</td> <td>sean</td> </tr> <tr> <td>年龄</td> <td>18</td> </tr> <tr> <td>性别</td> <td>男</td> </tr> <tr> <td>添加日期</td> <td>2016-01-01</td> </tr> <tr> <td>最后修改</td> <td>2016-03-01</td> </tr> </tbody> </table> </div> @endsection ~~~ ## 8.2 实现查看详情 ## 创建控制器中的 detail 方法: ~~~ /** * 根据id查看用户信息 * * @param * @return void * @author webjust [604854119@qq.com] */ public function detail($id) { $student = Student::find($id); return view('student/detail', ['student', $student]); } ~~~ 在视图中渲染变量:`\resources\views\student\detail.blade.php` ~~~ <?php use App\Models\Student; $students = new Student(); ?> @extends('layout/student') @section('content') <!-- 自定义内容区域 --> <div class="panel panel-default"> <div class="panel-heading">学生详情</div> <table class="table table-bordered table-striped table-hover "> <tbody> <tr> <td width="50%">ID</td> <td>{{ $student->id }}</td> </tr> <tr> <td>姓名</td> <td>{{ $student->name }}</td> </tr> <tr> <td>年龄</td> <td>{{ $student->age }}</td> </tr> <tr> <td>性别</td> <td>{{ $students->sex($student->sex) }}</td> </tr> <tr> <td>添加日期</td> <td>{{ date('Y-m-d', $student->created_time) }}</td> </tr> <tr> <td>最后修改</td> <td>{{ date('Y-m-d', $student->updated_time) }}</td> </tr> </tbody> </table> </div> @endsection ~~~ ## 8.3 实现删除 ## 在控制器中实现删除操作: ~~~ /** * 删除操作 * * @param * @return void * @author webjust [604854119@qq.com] */ public function delete($id) { $student = Student::find($id); if($student->delete()) { return redirect('/')->with('success', '删除成功-'.$id); } else { return redirect()->back()->with('error', '删除失败-'.$id); } } ~~~ 在删除时,添加一个提示窗口: <a onclick="if(confirm('确认删除吗?') == false) return false;" href="{{ url('student/delete', ['id' => $student->id]) }}">删除</a> 效果显示: ![](https://box.kancloud.cn/abcd463e178ccc6d796d61f09a624921_1046x728.png)