语法 | int delete(string $query, array $bindings = array()) |
---|---|
参数 |
|
Returns | int |
Description |
在数据库运行一个delete语句。
|
php artisan make:controller StudDeleteController
第3步 - 将以下代码复制到文件 app/Http/Controllers/StudDeleteController.php
app/Http/Controllers/StudDeleteController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; use App\Http\Requests; use App\Http\Controllers\Controller; class StudDeleteController extends Controller { public function index(){ $users = DB::select('select * from student'); return view('stud_delete_view',['users'=>$users]); } public function destroy($id) { DB::delete('delete from student where id = ?',[$id]); echo "删除记录成功.<br/>"; echo '<a href="/delete-records">点击这里</a> 返回.'; } }
resources/views/stud_delete_view.blade.php
<html> <head> <title>查看学生记录</title> </head> <body> <table border = "1"> <tr> <td>编号ID</td> <td>名字</td> <td>操作</td> </tr> @foreach ($users as $user) <tr> <td>{{ $user->id }}</td> <td>{{ $user->name }}</td> <td><a href = 'delete/{{ $user->id }}'>删除</a></td> </tr> @endforeach </table> </body> </html>
app/Http/routes.php
Route::get('delete-records','StudDeleteController@index'); Route::get('delete/{id}','StudDeleteController@destroy');
第7步 - 点击删除链接执行删除数据库中的记录(删除编号为 3 的记录)。您将被重定向到一个页面,会看到一条消息,如下面图中所示。