How to get data from database to view page in laravel?(如何从数据库中获取数据以在 laravel 中查看页面?)
问题描述
我正在使用 Laravel 5.4,我想从我的视图页面 (listpetani.blade.php) 中查看数据库中的数据.
I am using Laravel 5.4 and I want to view my data in database from my view page (listpetani.blade.php).
这是我项目的代码:
HTML:
<div class="table-responsive">
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th><strong>No</strong></th>
<th><strong>Nama Petani</strong></th>
<th><strong>Alamat</strong></th>
<th><strong>No. Handphone</strong></th>
<th><strong>Lokasi</strong></th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tbody>
</table>
</div>
PHP:在我的 listpetani.blade.php 我有一个空表,我想显示来自 数据库tbl_user:
PHP:
In my listpetani.blade.php I have an empty table and I want to show data from database tbl_user:
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
});
我页面中的表格:在浏览器中查看
我想在 laravel 5.4 中将数据库中的所有数据显示到我的视图中.有人可以帮帮我吗?
I want to show all the data from database into my view in laravel 5.4. Can anybody help me?
推荐答案
[SOLVE]
谢谢大家,这个问题我已经解决了
Thank you guys, I already solve this problem
这是解决的代码
web.php(路由)
web.php (routes)
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->get();
return view('listpetani', ['petani' => $petani]);
});
在我的 listpetani.blade.php
@foreach($petani as $key => $data)
<tr>
<th>{{$data->id_user}}</th>
<th>{{$data->nama_user}}</th>
<th>{{$data->alamat}}</th>
<th>{{$data->no_telp}}</th>
<th>{{$data->id_lokasi}}</th>
</tr>
@endforeach
这篇关于如何从数据库中获取数据以在 laravel 中查看页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从数据库中获取数据以在 laravel 中查看页面?
基础教程推荐
- php中的foreach复选框POST 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- php中的PDF导出 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
