Looping array data within blade - laravel-5

I make some API calls and fill up an array. I have identified three different ways an array element might come
array:9 [▼
0 => array:2 [▼
"jobData" => array:5 [▶]
"clientData" => array:3 [▶]
]
5 => array:3 [▼
"jobData" => array:5 [▶]
"clientData" => array:3 [▶]
"customData" => array:1 [▼
"Type of work" => array:1 [▼
"Type of work" => "Something"
]
]
]
8 => array:3 [▼
"jobData" => array:5 [▶]
"clientData" => array:3 [▶]
"customData" => array:2 [▼
"Month" => array:1 [▼
"Month" => "Aug 16"
]
"Type of work" => array:1 [▼
"Type of work" => "Something"
]
]
]
]
So Element 0 only has jobData and clientData. Element 5 has these two plus a cusomtData for Type of Work. Element 8 has everything including customData
for Type of Work and Month. Custom Data will either be completely empty, contain one, or contain both.
I am trying to display this data in a table. Most of it is simple enough apart from the custom data. At the moment I have something like this
<table class="table table-bordered table-hover additionalMargin alignment reportTable">
<tr class="col-md-12 noPadding">
<thead>
<tr>
<th>Job ID</th>
<th>Job Name</th>
<th>Category</th>
<th>Client Name</th>
<th>Type of work</th>
<th>Month</th>
</tr>
</thead>
</tr>
<tbody>
#if(is_array($jobArray))
#foreach($jobArray as $array)
<tr>
<td>{{ $array['jobData']['JobID'] or '' }}</td>
<td>{{ $array['jobData']['JobName'] or '' }}</td>
<td>{{ $array['jobData']['Category'] or '' }}</td>
<td>{{ $array['clientData']['ClientName'] or '' }}</td>
<td>{{ $array['customData']['Type of work'] or '' }}</td>
<td>{{ $array['customData']['Month'] or '' }}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
Now because the elements in my custom data are also arrays, this means I need to do another loop for them. So I could potentially do this
#if(!empty($array['customData']))
#foreach($array['customData'] as $data)
<td>{{ $data['Type of work'] or '' }}</td>
<td>{{ $data['Month'] or '' }}</td>
#endforeach
#endif
The problem with the above is that it seems to create its own tds and things are not matched up properly. I am essentially seeing something like this
How can I make things align up appropiately?
Thanks

You didn't prepare for next row of $array['customData'] that you have to fill with <tr>. Thats why you have all next results in the first column. Here is the solution:
#if(is_array($jobArray))
#foreach($jobArray as $array)
<tr>
<td>{{ $array['jobData']['JobID'] or '' }}</td>
<td>{{ $array['jobData']['JobName'] or '' }}</td>
<td>{{ $array['jobData']['Category'] or '' }}</td>
#if(!empty($array['customData']))
#foreach($array['customData'] as $i => $data)
#if($i > 0)
<tr>
<td colspan="3"></td>
#endif
<td>{{ $array['clientData']['ClientName'] or '' }}</td>
<td>{{ $data['Type of work'] or '' }}</td>
<td>{{ $data['Month'] or '' }}</td>
</tr>
#endforeach
#else
<td colspan="3"></td>
</tr>
#endif
#endforeach
#endif

This is a possible solution to your problem. What you're doing here is checking to see if there is any data in the "customData" object, as the other four objects are constant for all three object types.
<table class="table table-bordered table-hover additionalMargin alignment reportTable">
<tr class="col-md-12 noPadding">
<thead>
<tr>
<th>Job ID</th>
<th>Job Name</th>
<th>Category</th>
<th>Client Name</th>
<th>Type of work</th>
<th>Month</th>
</tr>
</thead>
</tr>
<tbody>
#if(is_array($jobArray))
#foreach($jobArray as $array)
#if(!empty($array['customData']))
#foreach($array['customData'] as $data)
<td>{{ isset($array['jobData']['JobID']) ? $array['jobData']['JobID'] : '' }}</td>
<td>{{ isset($array['jobData']['JobName']) ? $array['jobData']['JobName'] : '' }}</td>
<td>{{ isset($array['jobData']['Category']) ? $array['jobData']['Category'] : '' }}</td>
<td>{{ isset($array['clientData']['ClientName']) ? $array['clientData']['ClientName'] : '' }}</td>
<td>{{ isset($data['Type of work']) ? $data['Type of work'] : '' }}</td>
<td>{{ isset($data['Month']) ? $data['Month'] : '' }}</td>
#endforeach
#else
<td>{{ isset($array['jobData']['JobID']) ? $array['jobData']['JobID'] : '' }}</td>
<td>{{ isset($array['jobData']['JobName']) ? $array['jobData']['JobName'] : '' }}</td>
<td>{{ isset($array['jobData']['Category']) ? $array['jobData']['Category'] : '' }}</td>
<td>{{ isset($array['clientData']['ClientName']) ? $array['clientData']['ClientName'] : '' }}</td>
#endif
#endforeach
#endif
</tbody>
If $array['customData'] is not empty, it will populate the same four values for ID, Name, Category, and Client name for EACH unique type of work and/or month. If $array['customData'] is empty, then it will only print the ID, Name, Category and Client Name, then move onto the next Job ID.

Related

how to show combine Array in Datatable

I am new to Laravel and trying to show combinearray in the datatable but now able to get the output.
Code for Merged Array
$expdataforyear= array();
$expdataforyear = array_combine($getmonths, $expdata);
Array Output
"Apr-2021" => 0
"May-2021" => 0
"Jun-2021" => 0
"Jul-2021" => 0
"Aug-2021" => 0
"Sep-2021" => 0
"Oct-2021" => "285"
"Nov-2021" => "300"
"Dec-2021" => "250"
"Jan-2022" => "180"
"Feb-2022" => "315"
"Mar-2022" => "300"
Datatable
<table id="expensetable" class="table m-0 table table-bordered table-hover table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th>Months</th>
<th>Amount (Rs.)</th>
</tr>
</thead>
<tbody>
#foreach ($expdataforyearas $item )
<tr>
<td>{{$item->???}}</td>
<td>{{$item->???}}</td>
</tr>
#endforeach
</tbody>
</table>
Thanks in Advance
You can loop an array in your blade by using the Blade directive #foreach
Inside this directive you can prinbt out. The problem in your code comes from the syntax in the foreach function. #foreach($arr as $item) ... #endforeach
#foreach ($expdataforyearas as $itemKey => $itemValue )
<tr>
<td>{{ $itemKey }}</td>
<td>{{ $itemValue }}</td>
</tr>
#endforeach

how to change value change text in laravel

I want to when change TVA value change text But in the end, the same result
They appear "NET TTC" in both cases
Function :
public function invoice()
{
return $this->belongsTo(Invoice::class, 'invoice_id', 'id');
}
public function tvaText(){
if( $this->TVA_value == 0 ){
$this->tva_text='NET TTC';
}elseif($this->TVA_value != 0){
$this->tva_text='Total TTC';
}
return $this->tva_text;
}
Show :
#foreach($invoice->details as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->product_name }}</td>
<td>{{ $item->quantity }}</td>
<td>{{ $item->unit_price }}</td>
<td>{{ $item->row_sub_total }}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3"></td>
<th colspan="2">TVA value </th>
<td>{{ $invoice->TVA_value }}</td>
</tr>
<tr>
<td colspan="3"></td>
<th colspan="2">sub total</th>
<td>{{ $invoice->sub_total }}</td>
</tr>
<tr>
<td colspan="3"></td>
<th colspan="2">TVA total</th>
<td>{{ $invoice->TVA_total }}</td>
</tr>
<tr>
<td colspan="3"></td>
<th colspan="2">{{ $item->**tvaText()** }}</th>
<td>{{ $invoice->TTC_total }}</td>
</tr>
#endforeach
Please see pictures so that it becomes clear to you what I mean
does anyone know how to change text or any other method to achieve?
You can do it easily on blade.
#if($invoice->TVA_value == 0 )
<td>NET TTC</td>
#else
<td> Total TTC </td>
#endif

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

Laravel Foreach Looping

I need to display the previous value of the iterated array, here is code
<tr>
<th></th>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Delivery Address</th>
<th>Contact No.</th>
<th>Zip Code</th>
<th>Payment Terms</th>
<th>Credit Limit</th>
</tr>
<?php $previousValue = null; ?>
#foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue=$value->customer_name }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
#endforeach
I need to display the previous name of Customer in the iteration?
Any idea on how to do this?
Set value at the end , I set blank for first
#foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue or ""}}</td>
.....
.....
</tr>
<?php $previousValue = $value->customer_name ?>
#endforeach
$customer_count[$key - 1]
Of course you'll need to check if that exists or use the null-coalesce operator or something, so:
#php
$previous = $customer_count[$key - 1] ?? false;
#endphp
Then use:
#if( $previous )
... some stuff ...
#endif
wherever you need to inject.
please try this
<td>{{ (!$loop->first) ? $customer_count[$key - 1]->customer_name : '' }}</td>
You can read more about foreach loop here.
At very first set the previous value as blank and then at the bottom set value in $previousValue
<?php $previousValue = '';?>
#foreach($customer_count as $key => $value)
<tr>
<td>{{ $previousValue }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
<?php $previousValue = $value->customer_name; ?>
#endforeach

Resources