例子
步骤 1− 执行以下命令创建一个名为StudViewController.
php artisan make:controller StudViewController --plain
步骤 2− 成功执行步骤 1 后,您将收到以下输出 −
步骤 3- 将以下代码复制到文件中
app/Http/Controllers/StudViewController.php
app/Http/Controllers/StudViewController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StudViewController extends Controller {
public function index() {
$users = DB::select('select * from student');
return view('stud_view',['users'=>$users]);
}
}
步骤 4- 创建一个名为的视图文件resources/views/stud_view.blade.php并将以下代码复制到该文件中。
resources/views/ stud_view.blade.php
<html>
<head>
<title>View Student Records</title>
</head>
<body>
<table border = 1>
<tr>
<td>ID</td>
<td>Name</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
</tr>
@endforeach
</table>
</body>
</html>
步骤 5- 添加以下行app/Http/routes.php.
app/Http/routes.php
Route::get('view-records','StudViewController@index');
步骤 6− 访问以下 URL 以查看数据库中的记录。
http://localhost:8000/view-records
步骤 7− 输出将如下图所示。