maatwebsite excel multiple tables side by side - laravel

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.

Related

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

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>

show all subjects offered by a student using laravel eloquent

I have done one-to-many relationships but when I loop through it in my view only one subject displays even when I have more than one subjects.
This is what I have done in my controller
$broadsheet = Broadsheet::with(['subject', 'session', 'term', 'student', 'classarm'])
->where('session_id', $request->sessions)
->where('term_id', $request->terms)
->where('class_arm_id', $request->classarms)
->selectRaw('sum(total) as totals, count(subject_id) as countrow, student_id, subject_id, session_id, term_id, class_arm_id, total')
->groupBy('student_id')
->orderBy('totals', 'desc')
->get();
return view('broadsheet.index')->with('broadsheet', $broadsheet);
This is the index view
#extends('layouts.app')
#section('content')
{{--#foreach($broadsheet as $subjects)
#foreach ($subjects->subject as $sub)
<th> {{ strtoupper($sub->subject->subject_name) }}</th>
#endforeach
#endforeach--}}
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
#foreach($broadsheet as $subject)
<th> {{ strtoupper($subject->subject->subject_name) }}</th>
#endforeach
<th>TOTAL</th>
<th>AVERAGE</th>
<th>POSITION</th>
</tr>
</thead>
<tbody>
#foreach($broadsheet as $result)
<tr>
<td>{{ $result->total }}</td>
<td>{{ $result->totals }}</td>
<td>{{ number_format($result->totals/$result->countrow,2) }}</td>
<td>{{ $i++ }}</td>
<td>{{ $result->exams }}</td>
<td>{{ $result->total }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
and this is my Broadsheet schema
class Broadsheet extends Model
{
use HasFactory;
protected $fillable = [
'student_id',
'subject_id',
'session_id',
'term_id',
'class_arm_id',
'subject_total',
];
public function student()
{
return $this->belongsTo(Student::class, 'student_id');
}
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_id');
}
public function session()
{
return $this->belongsTo(Session::class, 'session_id');
}
public function term()
{
return $this->belongsTo(Term::class, 'term_id');
}
public function classarm()
{
return $this->belongsTo(ClassArm::class, 'class_arm_id');
}
}
I want to display students results like this
Intended result
Please, I would really need assistance to get this working as any help would be greatly appreciated.I just started learning laravel recently. I don't really know where the issue is coming from

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.

Resources