【问题标题】:Laravel display data from database table get() and first() methodsLaravel 显示来自数据库表 get() 和 first() 方法的数据
【发布时间】:2021-08-31 09:00:54
【问题描述】:

我想使用 get() 从数据库表中显示视图文件的数据...我收到此错误“此集合实例上不存在属性 [agent_name]”...但是如果我更改方法到 first() 我得到了表中第一个记录数据的意外输出。

这是我在 MiniStatementController 中的函数

  public function ministatements($id)
    {
      
        $mini_reports = DB::table('mini_statements')->where('user_id',$id)->get();
        $user = User::where('id',$id)->first();
 
        return view('vendor/voyager/reports/view_user_mini_statements')->with('mini_reports',$mini_reports)->with('user',$user);

        
    }

这是我的视图文件 view_user_ministatements.blade.php

@extends('voyager::master')




@section('page_header')
    <div class="container-fluid">
      
    </div>
@stop

@section('content')

    <div class="page-content browse container-fluid">
        @include('voyager::alerts')
        <div class="row">
            <div class="col-md-12">
                <div class="container">

                    <table class="table table-striped mt-5">
                        <thead>
                          <tr>
                            <th scope="col">#</th>
                            <th scope="col">Agent Name</th>
                            <th scope="col">Action</th>
                            <th scope="col">Amount</th>
                            <th scope="col">Time</th>
                          </tr>
                        </thead>
                        <tbody>
                         @if (is_array($mini_reports) || is_object($mini_reports))
                          @foreach ($mini_reports as $mini_report)    
                            <tr>
                              <th scope="row">1</th>
                              <td>{{$mini_reports->agent_name}}</td>
                              <td>{{\App\Action::where('id',$mini_reports->action)->first()->action_name}}</td>
                              <td>{{$mini_reports->amount}}</td>
                              <td>{{ \Carbon\Carbon::parse($mini_reports->created_at)->diffForHumans() }}</td>
                            </tr>
                          @endforeach
                          @endif
                        </tbody>
                      </table>
            
                </div>
            </div>
        </div>
    </div>

     
@stop
 

我如何在 web.php 文件中路由

Route::get('/{id}/{name}/view_user_mini_statements', 'MinistatementController@ministatements');

任何帮助请...谢谢。

【问题讨论】:

    标签: php mysql laravel-7


    【解决方案1】:

    Get返回一个对象数组,所以需要使用索引来获取。

    first() 方法将只返回一条记录,而 get() 方法将返回一个可以循环的记录数组。

    【讨论】:

    • 感谢您的回答,它似乎正在工作它只是迭代或重复第一个记录多次没有显示我需要显示的所有记录......你能详细说明如何正确地做到这一点。 ..再次感谢
    • 很高兴听到,请将其标记为正确答案
    • 在打印结果时使用此选项 $mini_report->agent_name
    • 它工作得非常好先生...谢谢,再次感谢...
    • 很高兴听到,如果它解决了您的问题,请将其标记为正确答案。
    猜你喜欢
    • 2018-10-30
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2013-10-27
    • 2017-04-26
    相关资源
    最近更新 更多