How to get total / sum before first row in php - php

Please help me, I need output total on top of data shown...
I have Looping like this in my blade.php
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
and output will like this:
And now how to get output total before firs row like image below?

The laravel collection has an inbuilt function called sum which returns the sum of Collection.
Learn here about collection function
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Total Pencil</td>
<td>{{ $datas->sum('amount') }}</td>
</tr>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>

convert object to array
$datasArray = json_decode(json_encode($datas), true);
collect all 'amount' in an array
$amountArray = array_column($datasArray,'amount');
Sum all element of $amountArray
$total = array_sum($amountArray );
print $total where you want

Related

Using DataTables in Laravel without aJax - No data available in table

I have a regular Laravel view that renders a table. When I try to use Datatables on it, it does render the table but it seems to want to format it before the table is actually rendered. I'm confused because, by the time the view is rendered, the server-side work is already done. The only thing it's doing is to render the table with the object passed from the controller.
<table id="stockmovement" class="table table-hover" style="width:100%">
<tbody>
<thead>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
<th colspan="3"></th>
</thead>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>
$(document).ready(function() {
$('#stockmovement').DataTable();
})
This is what I end up with:
I don't want to use AJAX to form my table. I've used plenty of vanilla PHP and DataTables instances before where it works just fine. I would appreciate any pointers on what I may be missing.
You have placed <tbody> in the wrong place. <tbody> should use under the </thead>.
Here is the code example:
<table id="stockmovement" class="table table-hover" style="width:100%">
<thead>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
</thead>
<tbody>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>
Add tr tag inside the thead tag.
The foreach loop should be in the opening and closing tbody tag.
It should look like this:
<table id="stockmovement" class="table table-hover" style="width:100%">
<thead>
<tr>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
</tr>
</thead>
<tbody>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>

How to group rows that have same Group IS as a foreign key?

I just trying to make a group of students who have the same group ID and calculate the CGPA of only those students who have belong to the same Group.
Refrence Example
Here's is the code for Controller
public function View_GroupStudent()
{
$allot_app = AllotmentApps::select(
"AllotmentApps.grpID",
"AllotmentApps.sname",
"AllotmentApps.cgpa"
)
->orderBy('grpID')
->get();
return view('deny', compact('allot_app'));
}
Views:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
<tr>
#foreach ($allot_app as $allot_app)
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ $allot_app->sum('cgpa') }}</td>
</tr>
#endforeach
</table>
Also help me in creating a proper view for that.
I was confused by your image because the red numbers weren't the averages at all. You should be able to achieve what you're trying to do with the following:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
#foreach ($allot_app->groupBy('grpID') as $grouped)
#foreach ($grouped as $allot_app)
<tr>
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ number_format($grouped->avg('cgpa'), 2) }}</td>
</tr>
#endforeach
#endforeach
</table>

How to get data from array in object in php?

In a database table - about_us, I have a column named subject
I am trying to make a table from this data in laravel
Column subject looks like this:
[
{
"name1": "1",
"name2": "2"
},
{
"name1": "3",
"name2": "4"
}
]
from controller
public function about()
{
$about = About::find(1);
return view('pages.about-us', compact('about'));
}
in about-us.blade.php
{{$about->subject}}
How can I extract the data to make a table like this?
The contents of your subject property is a JSON string. Which means that you will have to decode it through json_decode() into an array/object before your can iterate over it.
<table>
<thead>
<tr>
<th>right</th>
<th>left</th>
</tr>
</thead>
<tbody>
#foreach(json_decode($about->subject) as $subject)
<tr>
<td>{{ $subject->name1 }}</td>
<td>{{ $subject->name2 }}</td>
</tr>
#endforeach
</tbody>
</table>
Try code below:
<table>
<tr>
<td>right</td>
<td>left</td>
</tr>
#foreach(json_decode($about->subject) as $subject)
<tr>
<td>{{ $subject->name1 }}</td>
<td>{{ $subject->name2 }}</td>
</tr>
#endforeach
</table>
<table>
<thead>
<th><strong>right</strong></th>
<th><strong>left</strong></th>
</thead>
<tbody>
#foreach($about->subject as $subject)
<tr>
<td>{{ $subject->name1 }}</td>
<td>{{ $subject->name2 }}</td>
</tr>
#endforeach
</tbody>
</table>
Just loop over the data
Try this
<table>
<thead>
<tr>
<th>Right</th>
<th>Left</th>
</tr>
</thead>
<tbody>
#foreach($about->subject as $subject)
<tr>
<td>{{ $subject->name1 }}</td>
<td>{{ $subject->name2 }}</td>
</tr>
#endforeach
</tbody>
</table>

How to include all the data in foreach loop inside a table

#foreach($lastactivity as $activity)
{{ $activity }}
#endforeach
That is the for loop.
Now this is the output in the view.
{"id":2,"log_name":"index-log","description":"I visited index","subject_id":null,"subject_type":null,"causer_id":1,"causer_type":"App\\User","properties":[],"created_at":"2017-07-18 11:05:08","updated_at":"2017-07-18 11:05:08"}
How will I be able to put that in a table with all the columns and data arranged? Thank you very much.
You should use table.
<table>
<thead>
<tr>
<th>Log Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
#foreach($lastactivity as $activity)
<tr>
<td>{{$activity->log_name}}</td>
<td>{{$activity->description}}</td>
</tr>
#endforeach
</tbody>
</table>
In the same way, Add other fields as well.
You have to define your field name
{{ $activity->id }}
{{ $activity->log_name }}
etc
Like this:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Log Name</th>
.
.
.
<th>Updated At</th>
</tr>
</thead>
<tbody>
#foreach($lastactivity as $activity)
<tr>
<td>{{ $activity->id }}</td>
<td>{{ $activity->log_name }}</td>
.
.
.
<td>{{ $activity-> }}</td>
</tr>
#endforeach
</tbody>
</table>

How to us timestamp in query builder in laravel 5.3 to tack users registered in last 5 days

I want to list down the users who are registered in last 5 day.. To list down all the users 1 use following query..
$users = User::select('*')->orderby('created_at', 'desc')->get();
and fetch it in view like this.
<table class="table table-hover">
<tbody><tr>
<th>ID</th>
<th>User Name</th>
<th>Email</th>
<th>Country</th>
<th>Date</th>
<th>Points</th>
</tr>
<?php foreach ( $users as $u ){ ?>
<tr>
<td>{{ $u->id }}</td>
<td>{{ $u->user_name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->country }}</td>
<td>{{ $u->created_at }}</td>
<td>{{ \App\Points::getUserPoints($u->id) }}</td>
</tr>
<?php }?>
</tbody></table>
but what will be the query to fetch users who have registered in last 5 days..?? any one please tell me a good query..
You can use Carbon (docs) to do this:
$now = Carbon::now();
$users = User::where('created_at', '>=', $now->subDays(5))->get();
Make sure to "use Carbon\Carbon;" at the top of the file.

Categories