Show same values in same table cell - laravel

I have table with columns headers like 1, 2, 3, 4 and 5. Now I need to show products with quantity 1, 2, 3, 4 and 5 under each column, that is if there is a product with quantity 4, it will be shown under the column 4.
I have tried groupBy and foreach, but not getting expected result.
In my controller $products = Product::all();
In my view:
<table class="table">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
<tbody>
<tr>
#for($i = 1; $i < 6; i++)
#foreach($products as $product)
#if($product->quantity == $i)
<td>{{ $product->name }}</td>
#else
<td>{{ "-" }}</td>
#endif
#endforeach
#endfor
</tr>
</tbody>

Try this:
$products = Product::get()->groupBy('quantity');
And then in your table:
<table class="table">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
<tbody>
<tr>
#for($i = 1; $i <= 5; i++)
#if(!empty($products[$i]))
#foreach($products as $product)
<td>{{ $product->name }}</td>
#endforeach
#else
<td>-</td>
#endif
#endfor
</tr>
</tbody>

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>

how to show 3 Array in one datatable

I am trying to show 3 Array in one datatable.
// Combine Month Name and Data Array
$incomedataforyear = array();
$test1 = array_combine($getmonths, $incomedata);
$test2 = array_combine($getmonths, $animalselldata);
$test3 = array_combine($getmonths, $otherselldata);
$collection = collect([$test1, $test2, $test3]);
// End of Combine Month Name and Data Array
I also tried to use Collection but dont have any knowledge how to use this.
datatable code
<table id="incometable" class="table m-0 table table-bordered table-hover table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th>Month</th>
<th>Milk Sale Amount (Rs.)</th>
<th>Animal Sale Amount (Rs.)</th>
<th>Other Sale Amount (Rs.)</th>
</tr>
</thead>
<tbody>
#foreach ($expdataresults as $item )
<tr>
<td>00</td>
<td>00</td>
</tr>
#endforeach
</tbody>
</table>
Thanks in Advance
Formatting a bit different, you could use array_merge_recursive
$test1 = array_combine($getmonths, array_map(fn($i) => ['incomedata' => $i], $incomedata));
$test2 = array_combine($getmonths, array_map(fn($i) => ['animalselldata' => $i], $animalselldata);
$test3 = array_combine($getmonths, array_map(fn($i) => ['otherselldata' => $i], $otherselldata);
$collection = collect(array_merge_recursive($test1, $test2, $test3));
#foreach ($collection as $key => $value)
<tr>
<td>{{ $key }}</td>
<td>{{ $value['incomedata'] }}</td>
<td>{{ $value['animalselldata'] }}</td>
<td>{{ $value['otherselldata'] }}</td>
</tr>
#endforeach

How to sum table row data in laravel 8

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

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.

Print indexed array value in view.blade.php in laravel5.2

I have one associative array $collection and one indexed array $gnd which I have passed from Controller to my view.blade.php in Laravel 5.2. I want to print the values of both the arrays in a single table. Here is my code,
<table class="responsive-table highlight centered">
<thead>
<tr>
<th data-field="id">Sl.no</th>
<th data-field="name">Unique </th>
<th>Name</th>
<th data-field="price">Description</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{{-- */$j = 1;/* --}}
#foreach($collection as $key=>$value)
<tr>
<td>{{ $j }}</td>
<td>{{ $value->uid }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->desc }}</td>
<td>{{ $gnd[$j] }}</td>
{{-- */$j++;/* --}}
#endforeach
</tr>
</tbody>
</table>
For {{ $gnd[$j] }} I am getting the following error.
ErrorException in b7c1ab515a44988c31e1982a3ce014434e97ef2c.php line 30:
Undefined offset: 22 (View: /var/www/html/anudip/resources/views/plugins/entries/view.blade.php)
I am new in laravel. Please help me...
Function that passes the two arrays from Controller:
public function getDetails(){
$collection = DB::table('entry_transactions')
->leftJoin('entry_masters', 'entry_transactions.entry_id', '=', 'entry_masters.id')
->get(['entry_masters.id as uid','entry_masters.name as name','entry_masters.gender as gender','entry_transactions.entry_desc as desc']);
$gnd = array();
$len = sizeof($gnd);
$i = 0;
foreach ($collection as $key) {
if($key->gender == 0){
$gnd[$i] = "Male";
}
else {
$gnd[$i] = "Female";
}
$i++;
}
return view($this->url.'entries.view', compact('collection','gnd'));
}
The error you are getting because both array's size are not equal!
And why are you starting $j = 1? shouldn't be $j = 0, if both your arrays has the same size $j = 0 will fix your problem and change <td>{{ $j }}</td> to <td>{{ $j+1 }}</td>
Also, can you show us your controller function that is sending this two arrays?

Resources