use*_*352 1 php mysql laravel laravel-5
我想从名为“users”的数据库表中获取数据并在表中显示输出。
我写了下面的代码,但它不起作用。
我正在使用 Laravel 5.5
@extends('layouts.app')
@section('content')
<div class="container">
<h2>Admin Panel</h2>
<p>Data of the Registered Users.</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
$users = DB::table('users')->select('id','name','email')->get();
@foreach($users as $value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
Run Code Online (Sandbox Code Playgroud)
错误:未定义变量:用户
问题是您试图在(刀片)模板中混合 PHP。尽管使用@php指令可以这样做,但这是不好的做法:您的视图不应包含任何业务逻辑。
理想情况下,您希望从控制器传递此数据。它应该是这样的:
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function index()
{
$users = DB::table('users')->select('id','name','email')->get();
return view('some-view')->with('users', $users);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42620 次 |
| 最近记录: |