John Davidson

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

0 comments
Message:


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.



My Answer:
The issue in your view file is that you are trying to access the properties of the `$mini_reports` variable as if it were an object, but it is actually a collection of objects. When using the `get()` method in Laravel, it returns a collection of objects, so you need to loop through the collection and access the properties of each object within the loop.

Here is the updated code for your view file:

php
@extends('voyager::master')

@section('page_header')



@stop

@section('content')


@include('voyager::alerts')















@if (is_array($mini_reports) || is_object($mini_reports))
@foreach ($mini_reports as $key => $mini_report)







@endforeach
@endif

# Agent Name Action Amount Time
{{ $key + 1 }} {{ $mini_report->agent_name }} {{ \App\Action::where('id', $mini_report->action)->first()->action_name }} {{ $mini_report->amount }} {{ \Carbon\Carbon::parse($mini_report->created_at)->diffForHumans() }}







@stop


By making these changes, you should now be able to display the data from the database table using the `get()` method in Laravel.

Rate this post

5 of 5 based on 7185 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us