How to sum table row data in laravel 8 - laravel

I want to sum up the data in my table. Please tell, How can I do it. I have tried the below method but it is not working.
This is my controller code
public function CreateRentCertificateReport(Request $request)
{
$data['reports'] = Report::distric()->status(1)->get();
return view('adc.reports.start-create-report', $data);
}
This is my view code
<tbody>
#foreach($reports as $data)
<tr>
<td>{{$data->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$data->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
#foreach($reports as $data)
<td>{{$data['column_one']->countBy()}}</td>
#endforeach
</tr>
</tbody>

You can use pluck() and sum() :
<tbody>
#foreach($reports as $data)
<tr>
<td>{{ $data->upazila->upazila_name }}</td>
<td class="numeric_bangla">{{ $data->column_one }}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ $reports->pluck('column_one')->sum() }}</td>
</tr>
</tbody>

Maybe by doing something like this:
#php
$total = 0;
foreach($reports as $report) {
$total += $report['column_one']
}
#endphp
<tbody>
#foreach($reports as $report)
<tr>
<td>{{$report->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$report->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{$total}}</td>
</tr>
</tbody>

Instead of calling ->get() on an Eloquent query, you can call ->sum(“column_name”) to get a number which is the sum of that column’s values

Related

sum in blade how to do it for the subtotal of two tables?

i found out a way by someone asking a question, of a way of producing subtotals, i now just wanted to sum values between two tables , in the code from the person i tested and it works perfectly, i just wanted to aggregate more one table to any of the querys , it's summing on the blade.
//resumed controller
public function index() {
$emphayas = DB::select('select * from emphayas');
$treasuries = DB::select('select * from treasuries');
return view('database',['emphayas'=>$emphayas,
'treasuries'=>$treasuries,
]);}
-----------------------------------------------------------------
//resumed blade
<table class=" ">
<tr>
<th class="py-3 bg-cyan-400">Total</th>
<td class="py-3 bg-cyan-400">available_for_use_month</td>
</tr>
#foreach ($emphayas as $emphaya)
<tr>
<td></td>
<td>{{ $emphaya->available_for_use_month}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ \App\Models\Emphaya::sum('available_for_use_month') }}
</tr>
</table>
<table class=" ">
<tr>
<th class="py-3 bg-cyan-400">Total</th>
<td class="py-3 bg-cyan-400">available_for_use_month</td>
</tr>
#foreach ($treasuries as $treasury)
<tr>
<td></td>
<td>{{ $treasury->available_for_use_month}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ \App\Models\Treasury::sum('available_for_use_month') }}
</tr>
</table>
in this case what i want to do is connect the total of treasury to emphyas
<tr>
<td>Total</td>
<td>{{ \App\Models\Emphaya::sum('available_for_use_month') + \App\Models\Treasury::sum('available_for_use_month') }}
</tr>

Laravel #foreach Repeating

when I am trying to add #foreach 3 times it's repeating my data. how do I solve this?
this is my code
My Controller
$students = Student::with('FeeScheem','IndFeeScheem','FeeGroup','FeeGroup1')
->where('students.stu_id', $students)
->get();
$this->students = $students;
Eloquent Relationship
public function FeeScheem()
{
return $this->hasOne(FeeScheem::class, 'fs_id', 'stu_fee_scheem_id');
}
public function IndFeeScheem()
{
return $this->hasOne(IndividuallyFeeScheem::class, 'id', 'stu_ind_fee_scheem_id');
}
public function FeeGroup()
{
return $this->hasManyThrough(FeeMaster::class, FeeGroups::class);
}
public function FeeGroup1()
{
return $this->hasMany(FeeGroups::class, 'individually_fee_scheem_id', 'stu_fee_scheem_id');
}
My Blade File
#foreach ($students as $stu=>$student)
#foreach ($student->FeeGroup as $key=>$FeeG)
#foreach ($student->FeeGroup1 as $key1=>$FeeG1)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $FeeG1->fee_group_name }}</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
#endforeach
#endforeach
#endforeach
</tbody>
This is a preview
Click here to preview
You can use the index property of the loop variable :
#foreach ($students as $stu=>$student)
#if($loop->index < 2)
// Your code
#endif
#endforeach
you closed the marker incorrectly. should be something like:
#foreach ($students as $stu=>$student)
#foreach ($student->FeeGroup as $key=>$FeeG)
<tr>
<td></td>
</tr>
#endforeach
#foreach ($student->FeeGroup1 as $key1=>$FeeG1)
<tr>
<td>{{ $FeeG1->fee_group_name }}</td>
<td></td>
</tr>
#endforeach
#endforeach
</tbody>

How to display this unserialize data to blade in Laravel

public function getcustomerorder($id)
{
$orderss = Orders::find($id);
$orders = $orderss->all();
$orders->transform(function($order){
$order->cart = unserialize($order->cart);
return $order;
});
return $orders;
}
The output of that from the controller is below:
[{"id":3,"cart":{"1":{"itemcode":"20062872","itemname":"AXE BS TWIST 50ML","quantity":4,"price":125},"2":{"itemcode":"20062881","itemname":"DOVE RO ORIGINAL 40ML","quantity":5,"price":50}},"firstname":"Joseph Vincent","lastname":"Limbaroc","phonenumber":"09197963942","email":"joseph.coquilla#outlook.com","street":"094 B Brgy. 40-D Bolton Ext. St.","street2":null,"city":"Davao City","province":"Davao Del Sur","zip":"8000","status":"new","created_at":"2020-04-20T05:15:09.000000Z","updated_at":"2020-04-20T05:15:09.000000Z"}]
How do I display the array of cart into blade using
#foreach($orders as $item)
<tr>
<td>{{ }}</td>
</tr>
#endforeach
Hi Sarvil you could change the structure of your JSON a little. You can make "cart" to be an array not an object but I hope this syntax will help you:
#foreach($orders as $item)
<tr>
<td>{{$item->id}}</td>
</tr>
#foreach($item->cart as $item_cart)
<tr>
<td>{{$item_cart->itemcode}}</td>
</tr>
<tr>
<td>{{$item_cart->itemname}}</td>
</tr>
#endforeach
<tr>
<td>{{$item->firstname}}</td>
</tr>
#endforeach

Nested Looping in Laravel Blade Templating

Here is my Controller
When I run this controller code I get the result as in the screenshot below, as all data is correct.
$query = DB::table('intis_auctions_lots')
->select('bidincrement','intis_lot_id','lot','product_name','maxbid','enddate')
->where([['community_name','=',$CommunityNo],['SellerId','=',$SellerId],['active','=','Y'],['startdate','<',$now],['enddate','>',$now]])
->orderBy('intis_lot_id')
->get();
foreach($query as $querys) {
$qintis_lot_id = $querys->intis_lot_id;
$qenddate = date('H:i', strtotime($querys->enddate));
$individualbids = DB::table('intis_auctions_lots')
->select(DB::raw('sum(nrbids) as nbid,lot, startprice,intis_lot_id,product_name,enddate,maxbid'))
->where([['community_name','=',$CommunityNo],['SellerId','=',$SellerId],['active','=','Y'],['startdate','<',$now],['enddate','>',$now],['intis_lot_id','=',$qintis_lot_id]])
->groupBy('startprice','intis_lot_id','product_name','lot','enddate','maxbid')
->get();
foreach($individualbids as $key => $value)
echo "<tr><td>".$value->lot."</td>
<td>".$value->intis_lot_id."</td>
<td>".$value->product_name."</td>
<td>".date('H:i', strtotime($value->enddate))."</td>
<td>".$value->startprice."</td>
<td>".$value->maxbid."</td>
</tr>";
}
}
My View code as below
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Lot</th>
<th>Product Name</th>
<th>End Time</th>
<th>Bids</th>
<th>Start Price</th>
<th>Current Bid Amount</th>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($query as $key=>$value)
#foreach ($individualbids as $key => $value1)
<tr>
<td>{{ $value->lot }}</td>
<td style="text-align:left">{{ $value->intis_lot_id }},{{ $value->product_name }}</td>
<td>{{ date('H:i', strtotime($value->enddate)) }}</td>
<td></td>
<td>{{ $value1->startprice }}</td>
<td>{{ $value->maxbid }}</td>
<td></td>
<td></td>
</tr>
#endforeach
#endforeach
</tbody>
<tfoot>
<tr>
</tr>
</tfoot>
</table>
</div>
When pass it to the view I'm getting result as in the screen below, as the Starting Price as 340 it takes the first value to all the row as same value, but when I run in the controller code without passing to view it show correct value. Please provide me the solution to write the proper code in the view, as it will helpful to me.

maatwebsite excel multiple tables side by side

I have code that create spreadsheet from Maatwebsite, I use Laravel 5.7,
The code I have is described as below:
export.blade.php
<table>
<thead>
<tr><th colspan="{{ $colspan }}">{{ $monthText }}</th></tr>
<tr>
#foreach($theader as $thead)
#if($thead == "Email")
#continue
#endif
<th>{{ $thead }}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach($records as $record)
<tr>
#foreach($record as $key=>$value)
#if($key == "Email")
#continue
#endif
<td>{{ $value }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
<table>
<thead>
<tr><th colspan="{{ $colspan }}">Month to Date (MTD)</th></tr>
<tr>
#foreach($theader as $thead)
#if($thead == "Email")
#continue
#endif
<th>{{ $thead }}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach($records as $record)
<tr>
#foreach($record as $key=>$value)
#if($key == "Email")
#continue
#endif
<td>{{ $value }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
The duplicate table is just for testing how it will be displayed in Excel when it finish exporting, using this code
class TopFiveExport implements FromView, WithTitle
{
protected $data;
protected $sheetTitle;
function __construct($result,$parameters)
{
$this->sheetTitle = $parameters['title'];
$this->data['theader'] = array_keys($result[0]);
$this->data['records'] = $result;
$this->data['colspan'] = $parameters['colspan'];
$this->data['monthText'] = $parameters['monthText'];
}
public function view(): View
{
return view('exports.backoffice.top5',$this->data);
}
public function title(): string
{
return $this->sheetTitle;
}
}
Any idea on how to get excel exporting tables side by side, currently, I get the two tables below each other, I want them to be side by side.

Resources