【问题标题】:How to get data from database to view page in laravel?如何从数据库中获取数据以在 laravel 中查看页面?
【发布时间】:2017-08-22 17:42:09
【问题描述】:

我正在使用 Laravel 5.4,我想从我的查看页面 (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 中,我有一个空表,我想显示来自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]);

});

还有我页面中的表格:view in browser

我想在 laravel 5.4 中将数据库中的所有数据显示到我的视图中。有人可以帮帮我吗?

【问题讨论】:

  • 在返回查看之前可以使用 did($petani); 来检查结果集吗

标签: php mysql laravel fetch laravel-5.4


【解决方案1】:

您也可以在视图中从数据库中获取数据

@php( $contacts = \App\Contact::all() )  
@php( $owners = \App\User::all())  

<select class="form-control" name="owner_name" id="owner_name">                           
    @foreach($contacts as $contact)
      <option value="{{ $contact->contact_owner_id }}">{{ $contact->contact_owner }}</option>
    @endforeach                            
</select>  

如果你将数据从控制器传递到视图会更好。

return view('greetings', ['name' => 'Victoria']);

查看文档: https://laravel.com/docs/8.x/views#passing-data-to-views

【讨论】:

  • 您可以通过紧凑功能从控制器发送数据,例如: $owners = \App\User::all() return view('viewowners', compact('owners'));
【解决方案2】:

我希望您已经知道这一点,但也可以尝试使用 Model 和 Controller, 让路由接受请求,并将其传递给控制器 并使用 Eloquent,这样你的代码就可以这么短:

$petani = PetaniModel::all();
return view('listpetani', compact('petani));

而不是使用foreach,使用forelse@empty 语句以防'listpetani' 为空,并向视图提供一些信息,例如“列表为空”。

【讨论】:

    【解决方案3】:

    在您的控制器中:

     $select = DB::select('select * from student');
     return view ('index')->with('name',$select);
    

    在你看来:

    @foreach($name as $data)
    <tr>
        <th>{{ $data->id}}</th> <br>
        <th>{{ $data->name}}</th> <br>
        <th>{{ $data->age}}</th> <br>
        <th>{{ $data->address}}</th>
    </tr>
    @endforeach
    

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      或者你可以在 laravel Blade 中使用 @forelse 循环

      @forelse($name as $data)
      <tr>
          <th>{{ $data->id}}</th>
          <th>{{ $data->name}}</th>
          <th>{{ $data->age}}</th>
          <th>{{ $data->address}}</th>
      </tr>
      @empty
          <tr><td colspan="4">No record found</td></tr>
      @endforelse
      

      【讨论】:

        【解决方案5】:

        其他答案是正确的,我只想再补充一件事,如果您的数据库字段有html标签,那么系统将打印html标签,如“”作为段落标签。要消除此问题,您可以使用以下解决方案:

        您可能需要将 html_entity_decode 应用于您的数据。

        这适用于 Laravel 4

        {{html_entity_decode($post->body)}}

        对于 Laravel 5,您可能需要改用它

        {!!html_entity_decode($post->body)!!}

        【讨论】:

          【解决方案6】:

          [解决]

          谢谢大家,这个问题我已经解决了

          这是解决的代码

          web.php(路由)

          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
          

          【讨论】:

          【解决方案7】:
          @foreach($petani as $p)
           <tr>
               <td>{{ $p['id_user'] }}</td>
               <td>{{ $p['username'] }}</td>
               <td>{{ $p['alamat'] }}</td>
               <td>{{ $p['no_telp'] }}</td>
               <td>{{ $p['id_lokasi'] }}</td>
           </tr>
          @endforeach
          

          【讨论】:

          • 试图获取非对象的属性
          • 对所有值尝试 $p['id_user'] 而不是 $p->id _user
          【解决方案8】:

          **在侧控制器中你传递这个**:

          $petanidetail = DB::table('tb1_user')->get()->toArray();
          return view('listpetani', compact('petanidetail'));
          

          在内部视图中,您使用 petanidetail 变量如下

          foreach($petanidetail as $data)
          {
          echo $data;
          }
          

          【讨论】:

          • 未定义变量:petanidetail 如果我根据您的建议进行更改,则会出现错误消息
          • 你应该在控制器中传递 petanidetail 并返回如下视图
          【解决方案9】:

          试试这个:

          $petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
          return view('listpetani', ['petani' => $petani]);
          

          在您的视图中使用foreach() 迭代$petani,例如:

          foreach($petani as $data)
          {
              echo $data->id_user;  // $petani is a Std Class Object here
              echo $data->username;
          }
          

          【讨论】:

            猜你喜欢
            • 2021-05-11
            • 1970-01-01
            • 2019-05-24
            • 2021-10-19
            • 1970-01-01
            • 1970-01-01
            • 2012-02-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多