I want to display data on view file from database table using get()...and i get this error ''Property [agent_name] does not exist on this collection instance''...but if i change the method to first() i get the unexpected output of first recorded data in a table.
this is my function in 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);
}
And this is my view file 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
How i routed in web.php file
Route::get('/{id}/{name}/view_user_mini_statements', 'MinistatementController@ministatements');
Any help please...Thank you.
Get returns an array of object, so you need to use index to get it.
The first() method will return only one record, while the get() method will return an array of records that you can loop over.