Laravel 显示来自数据库的数据 table get() 和 first() 方法

Laravel display data from database table get() and first() methods

我想使用 get() 在数据库 table 的视图文件上显示数据...我收到此错误“'Property [agent_name] does not exist on this collection instance'”...但是如果我将方法更改为 first () 我在 table.

中得到了第一个记录数据的意外输出

这是我在 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');

任何帮助...谢谢。

获取returns一个对象数组,需要使用索引获取。

first() 方法将 return 只有一条记录,而 get() 方法将 return 一组可以循环的记录。