如何从数据库中获取数据以在 laravel 中查看页面? [英] How to get data from database to view page in laravel?

查看:35
本文介绍了如何从数据库中获取数据以在 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 中查看页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆