Laravel #foreach Repeating - laravel

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>

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

ASK I want to use 2 FOREACH in 1 table body VIEW laravel

I want to use 2 FOREACH in 1 table body VIEW laravel
like this for the EXCEL.VIEW.BLADE code
<table>
<thead>
<tr>
<th>Booking Status</th>
<th>CreatedAT</th>
<th>New row</th>
</tr>
</thead>
<tbody>
#foreach($datas as $hasil)
foreach($pp as $res)
<tr>
<td>{{ $hasil->BookingStatus }}</td>
<td>{{ $hasil->DeliveryCharge }}</td>
<td>{{ $res->BookingStatus }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
and for the code in the controller like this
$data = TR_BookingHeader::where('LogisticId', $this->qty)->get();
$pp = TR_BookingHeader::all();
return view('Excel', [
'datas' => $data, 'pp' => $pp
]);
Try this one
<tbody>
#foreach($datas as $hasil)
#foreach($pp as $res)
<tr>
<td>{{ $hasil->BookingStatus }}</td>
<td>{{ $hasil->DeliveryCharge }}</td>
<td>{{ $res->BookingStatus }}</td>
</tr>
#endforeach
#endforeach
</tbody>
Another example
[
state
college
college
state
college
college
college
...
]
#foreach ($states as $state => $colleges)
<h1 style="center">{{ $state }}</h1>
<ul class="nospace clear">
#foreach ($colleges as $college)
<li class="one_quarter first">
<h3 class="center">{{ $college->college }}</h3>
</li>
#endforeach
</ul>
#endforeach

Laravel - syntax error, unexpected ')', expecting '['

I am using Laravel 5.8 to create an App. When I wanted to display the view, it generated an error as shown below:
syntax error, unexpected ')', expecting '['
Controller
public function serviceOverview(Request $request)
{
$data['title'] = 'Subscription Overview';
$serviceoverviews = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
$render=[];
if(isset($request->package_name))
{
$serviceoverviews=$serviceoverviews->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
$render['package_name']=$request->package_name;
}
if(isset($request->service_id))
{
$serviceoverviews=$serviceoverviews->where('service_id',$request->service_id);
$render['service_id']=$request->service_id;
}
$serviceoverviews= $serviceoverviews->orderBy('created_at','DESC');
$serviceoverviews= $serviceoverviews->paginate(15);
$serviceoverviews= $serviceoverviews->appends($render);
$data['serviceoverviews'] = $serviceoverviews;
return view('report.serviceOverview',$data);
}
In the Controller, I tried to do some filtering. Also, I did raw query.
View
<div class="box box-primary">
<div class="box-header with-border">
#if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
#endif
#if(count($detailsubscriptions))
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->links() }}
</td>
</tr>
</tbody>
</table>
#else
<div class="row text-center">
<h2>No Service Overview to show</h2>
</div>
#endif
</div>
</div>
I tried to check the code, but found nothing. How do I resolve this issue
You have missed a $ from the $serviceoverview variable in your blade #foreach. Change:
#foreach($serviceoverviews as $key => serviceoverview)
To
#foreach($serviceoverviews as $key => $serviceoverview)
Here is the full code updated with changes. Please refer to this
Controller.php
public function serviceOverview(Request $request)
{
$builder = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
if($request->filled('package_name'))) {
$builder = $builder->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
}
if(isset($request->service_id))
{
$builder=$builder->where('service_id',$request->service_id);
}
$serviceoverviews = $builder->orderBy('created_at','DESC')->paginate(15);
$data = [
'title' => 'Subscription Overview',
'serviceoverviews' => $serviceoverviews
];
return view('report.serviceOverview', $data);
}
Now change your view code like this.
view.blade.php
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => $serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->appends(\Request::all())->links() }}
</td>
</tr>
</tbody>
</table>
You are using serviceoverview in place of $serviceoverview in view file. That would be an issue.
You have made some typo and one logical error.
Change
#foreach($serviceoverviews as $key => serviceoverview) to #foreach($serviceoverviews as $key => $serviceoverview)
<td>{{ serviceoverview->service_name }}</td> to <td>{{ $serviceoverview->service_name }}</td>
You have put {{ $serviceoverview->links() }} out side of the foreach loop

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.

Laravel nested foreach?

Having trouble with nested foreach and despite the other questions, I can't get my head around what could be wrong with my Customers.blade file:
#foreach ($customers as $company)
<tr>
<td></td>
<td>{{ $company->name }}</td>
<td>#if ($company->active == 1) YES #else NO #endif</td>
<td>View</td>
<td>Edit</td>
<td>Remove</td>
</tr>
#foreach ($customersites->customer_id as $company->id)
<tr>
<td></td>
<td>{{ $customersites->sitename }}</td>
<td>#if ($customersites->active == 1) YES #else NO #endif</td>
<td>View</td>
<td>Edit</td>
<td>Remove</td>
</tr>
#endforeach
#endforeach
This results in:
Property [customer_id] does not exist on this collection instance. (View: >C:\Apache24\htdocs\truckrun\resources\views\customers.blade.php)
What I'm doing wrong?
Please try this:
#foreach ($customers as $company)
<tr>
<td></td>
<td>{{ $company->name }}</td>
<td>#if ($company->active == 1) YES #else NO #endif</td>
<td>View</td>
<td>Edit</td>
<td>Remove</td>
</tr>
#foreach ($customersites as $company->id)
<tr>
<td></td>
<td>#if(isset($customersites->sitename) && $customersites->sitename !="") {{ $customersites->sitename }} #endif</td>
<td>#if ($customersites->active == 1) YES #else NO #endif</td>
<td>View</td>
<td>Edit</td>
<td>Remove</td>
</tr>
#endforeach
#endforeach

Resources