Display multiple variable data separately from multiple tables on single view in blade template in laravel - laravel-5

I want to display two variable data on same view in a foreach loop
public function getWalletList()
{
$data = Data::orderBy('id','desc')->get()->toArray();
$user_ids = array_column($data, 'id');
$user_data = User::whereIn('id',$user_ids)->get()->toArray();
$d= array('data'=>$data,'user'=>$user_data);
return view('wallet_list', compact('d'));
}
So how to diplay data on laravel blade template
#foreach($d as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
</tr>
#endforeach

why dont you use two foreach ?
#foreach($d as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
</tr>
#endforeach
#foreach($b as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
<tr>
#endforeach

Related

Get sum of minutes of specific project

I want to get total minutes. Example: joylinkhk 240 + 180 = 420. How can I get a total minute of a specific user?
controller
public function search(Request $request) {
$date = explode(' - ', $request->date);
$auth = Auth::user();
$hourLog = Hourlog::with('project', 'user');
if ($auth->user_type == 1) {
$hourLog->where("user_id", $auth->id);
}
$data = [
"date" => $date,
// 'projects' => Project::whereIn('id', $request->project)->get(),
'projects' => Project::with('users')->whereIn('id', $request->project)->get(),
'users' => User::with(['hourlog' => function ($query) use ($request) {
$query->whereIn('project_id', $request->project);
}])->whereIn('id', $request->user)->get(),
];
return view('cms.projectreport.projectreport-list', $data);
}
HTML view
#foreach($users as $user)
<tr>
<td>{{$user->name}}
</td>
#foreach($user->hourlog as $hourlogs)
<td>{{$hourlogs->hour_work}}
</td>
#endforeach
</tr>
#endforeach
Laravel collections are very powerful and offer such functionality out of the box.
Below is a example of how you can take a collection of arrays and sum by its property hour_work. The collect function is a quick way to create a collection like the $user->hourlog already is.
collect([
['hour_work' => 198],
['hour_work' => 93],
['hour_work' => 51],
['hour_work' => 112],
])->sum('hour_work');
// Result: 454
As #danny-van-der-sluijs said, you can use sum on a collection
#foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
#foreach($user->hourlog as $hourlogs)
<td>{{$hourlogs->hour_work}}</td>
#endforeach
</tr>
#endforeach
<tr>
<td>Grand Total</td>
<td>{{ $users->sum(fn($user) => $user->hourlog->sum('hour_work')) }}</td>
</tr>
Note:
$users->sum(fn($user) => $user->hourlog->sum('hour_work'))
Is an arrow function that came with php 7.4, If you are using a version below 7.4 you have to do it like this
$users->sum(function($user) {
return $user->hourlog->sum('hour_work');
});
You can sum in foreach loop, but get the result outside of foreach loop. You need to define first the variable outside of foreach $sum_total = 0;, then you can sum inside foreach like this :
<?php $sum_total = 0; ?> // define here
#foreach($users as $user)
<tr>
<td>{{$user->name}}
</td>
#foreach($user->hourlog as $hourlogs)
<td>{{$hourlogs->hour_work}}
</td>
<?php
$sum_total += (intval)$hourlogs->hour_work
?>
#endforeach
</tr>
<tr>
<td>{{ $sum_total }}</td>
</tr>
#endforeach

Display list of purchase order base from filtered vendor in Laravel eloquent

I am trying to display the list of vendor with their purchase orders in a table.
VENDOR MODEL
public function vendorPo()
{
return $this->hasMany('App\PurchasedOrder', 'vendor_id','vendor_id');
}
VENDOR CONTROLLER
public function vendor()
{
$vendorPo = Vendor::with('vendorPo')
->get()
->keyBy('id')
->groupBy('vendor_name');
$purchaseOrders = PurchasedOrder::all()->where('publish','=','1');
return view('backend.purchase-order.vendor', compact('purchaseOrders'))
->with('vendorPo', $vendorPo);
}
for my index
#foreach ($vendorPo as $vendor => $vendor_list)
<tr>
<th colspan="7"
style="background-color: #F7F7F7"> {{ $vendor }}: ({{ $vendor_list->count() }} vendors)
</th>
</tr>
#foreach ($vendor_list as $key => $vendorPoList)
<tr>
<td>
{{ $vendorPoList->vendorPo->po_id}}
</td>
</tr>
#endforeach
#endforeach
if I do this
...
#foreach ($vendor_list as $key => $vendorPoList)
<tr>
<td>
{{ $vendorPoList}}
</td>
</tr>
#endforeach
...
the result is this
and if I
...
<td>
{{ $vendorPoList->vendorPO->po_id}}}
</td>
...
the result is error
Facade\Ignition\Exceptions\ViewException
Property [po_id] does not exist on this collection instance. (View:
Please help me. Thanks!
You are getting this issue because your vendorPO relation is a HasMany relation. If you pull back the records from that sort of relation, the results are always an array (even if there is only one record), which is always converted by Eloquent into a Collection.
Therefore, $vendorPoList->vendorPO is a Collection, and there is no variable on a Collection called po_id.
Depending on your data model, you should either revisit the relation, or do a #foreach on $vendorPoList->vendorPO.

Converting objects to arrays

I'm trying to paginate csv file and I succeeded with it's data, but I couldn't with the file's header(keys). The error says array_keys expected() to be array but objects given, so I wanna to convert array_keys() to array.
I tried to use toArray() function but useless:
public function indexPagination()
{
$products = Product::all();
$products = $products->toArray();
$products = Product::paginate(5);
return view ('inventory.layout', ['products'=>$products]);
}
The error commes from this section who is gonna shows the file's keys, but error says array_keys() expecting to be array but objects given.... please help
<thead>
#if ($arrkeys= array_keys($products[0]))
#foreach ($arrkeys as $keys)
<th>{{$key}}</th>
#endforeach
#endif
</thead>
It is not very clear exactly what your desired result is. It looks like you are trying to paginate over the table headers. If this is correct, the following might suffice:
Controller
return view ('inventory.layout', [
'products' => Product::paginate(5),
]);
View
<table>
<thead>
#foreach ($products as $product)
<th>{{ $product->name }}</th>
#endforeach
</thead>
</table>
{{ $products->links() }}
It worked well now through convertin the parameter of array_keys() to array using toArray() function because it was an object before. this is the answer:
#if ($arrkeys= array_keys(($products[0])->toArray()))
#foreach ($arrkeys as $key)
<th>{{$key}}</th>
#endforeach
#endif

how to get data from laravel5.5?

I am using Laravel 5.5 and I want to view my data in database from my view page (home.blade.php)
<table>
<tbody>
#foreach($home as $key => $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
</tbody>
</table>
In my home.blade.php I have an empty table and I want to show data from database fyp:
Route::get('home', function () {
$fyp = DB::table('reports')->get();
return view('home', ['fyp' => $fyp]);
});
This must work (as #Nazmul said):
#foreach($fyp as $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
just follow the code
Route::get('home', function () {
$home = DB::table('reports')->get();
return view('home', compact('home'));
});
After in your view page
<table>
<tbody>
#foreach($home as $key => $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
</tbody>
</table>
I hope this will help you.

Trying to pass data from controller to view in laravel 5

I am trying to pass 2 different columns from my sql table to my view ,so that i can show them.
My controller is:
$interface = DB::table("users")->distinct()->get(['name']);
$interfac = DB::table("users")->distinct()->get(['email']);
view()->share('users',compact('interface','interfac'));
My view is :
#foreach ($users as $key => $value)
<tr>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
</tr>
But this is not working while passing the two variables.If i pass one it is working but passing two is not working.thanks.
Controller
$users = DB::table('users')->get();
return view('users', compact('users'));

Resources