[英]Fetching data from Database in Laravel
I want to fetch data from database table named 'users' and display the output in a table.我想从名为“用户”的数据库表中获取数据,并在表中显示 output。
I have written below code but it's not working.我写了下面的代码,但它不工作。
I am using Laravel 5.5我正在使用 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
Error: Undefined Variable: users
错误:未定义变量:用户
The problem is that you're trying to mix PHP within your (Blade) template. 问题是您正在尝试在(刀片)模板中混合使用PHP。 Although it is possible to do so with
@php
directives, this is bad practice: your view should not contain any business logic. 尽管可以使用
@php
指令执行此操作,但这是一个不好的做法:您的视图不应包含任何业务逻辑。
Ideally you want to pass this data from your controller. 理想情况下,您要从控制器传递此数据。 It should look something like this:
它看起来应该像这样:
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);
}
}
Read more on passing data to views here . 在此处阅读有关将数据传递到视图的更多信息 。
You are doing it all wrong, the point of MVC
design is to have your controllers
do the application logic, your views to represent a representation of that data
and models to contain the data you need
, in your case you are missing the point completely by using DB::table
inside of your view, so here is an example code which you might need to correct a bit : 您做错了所有事情,
MVC
设计的重点是让controllers
执行应用程序逻辑,您的视图representation of that data
的representation of that data
并contain the data you need
模型,在这种情况下,您完全错过了要点在视图内部使用DB::table
,因此这是一个示例代码,您可能需要对其进行一些纠正 :
The example below doesn't show MVC
pattern since the data
is passed from inside a closure of a route
下面的示例未显示
MVC
模式,因为data
是从route
的闭合内部传递的
web.php web.php
Route::get('/', function () {
$users = DB::table('users')->select('id','name','email')->get();
return view('VIEW-NAME-HERE', compact('users'));
});
view.blade.php view.blade.php
@foreach($users as $user)
{{ $user->id }} {{ $user->name }} {{ $user->email }}
@endforeach
Change VIEW-NAME-HERE
with the name of your view
file, for example index
or users.index
用
view
文件的名称更改VIEW-NAME-HERE
,例如index
或users.index
You Are using php in blade file first make controller for controller run a command in terminal php artisan make:controller usercontroller您在刀片文件中使用 php 首先为 controller 制作 controller 在终端 php artisan make:controller usercontroller 中运行命令
then write this:然后这样写:
class usercontroller extends Controller
{
public function index()
{
$users = DB::table('users')->select('id','name','email')->get();
$data = compact('users')
return view('your-view-name')->with('$data');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.