例子
步骤 1− 执行以下命令创建一个名为StudDeleteController.
php artisan make:controller StudDeleteController --plain
步骤 2− 成功执行后,您将收到以下输出 −
步骤 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 "Record deleted successfully.<br/>";
echo '<a href = "/delete-records">Click Here</a> to go back.';
}
}
步骤 4- 创建一个名为的视图文件
resources/views/stud_delete_view.blade.php并将以下代码复制到该文件中。
resources/views/stud_delete_view.blade.php
<html>
<head>
<title>View Student Records</title>
</head>
<body>
<table border = "1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Edit</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td><a href = 'delete/{{ $user->id }}'>Delete</a></td>
</tr>
@endforeach
</table>
</body>
</html>
步骤 5- 添加以下行app/Http/routes.php.
app/Http/routes.php
Route::get('delete-records','StudDeleteController@index');
Route::get('delete/{id}','StudDeleteController@destroy');
步骤 6- 输出将如下图所示。
步骤 7− 单击删除链接可从数据库中删除该记录。您将被重定向到一个页面,您将在其中看到一条消息,如下图所示。
步骤 8− 单击“Click Here”链接,您将被重定向到一个页面,您将在其中看到除已删除记录之外的所有记录。