語法 | 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 的記錄)。您將被重定向到一個頁面,會看到一條訊息,如下面圖中所示。