如何从数据库中获取数据以在 laravel 中查看页面?

2022-01-20 00:00:00 fetch php mysql laravel laravel-5.4

我正在使用 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

相关文章