Laravel Blade - Displaying array content - laravel

I make calls to some APIs to fill up an array. The end output is something like the following
Array
(
[0] => Array
(
[leadData] => Array
(
[LeadID] => 1232806
[DateIdentified] => 21/04/2016
[Client] => Prospect 1
[LeadName] => Test
[Owner] => Some Owner
[Value] => 2160.00
[Status] => 70%
)
[clientData] => Array
(
[BusinessStructure] =>
[IsProspect] => No
)
[quoteData] => Array
(
[QuoteID] => Q0020
[ProjectName] => Test
[Amount] => 1800.00
[AmountTax] => 360.00
[AmountIncludingTax] => 2160.00
[EstimatedCost] => 450.00
[EstimatedCostTax] => 90.00
[EstimatedCostIncludingTax] => 540.00
)
[customData] => Array
(
[0] => Array
(
[Lead Type] => New
)
[1] => Array
(
[Month] => June
)
)
)
[1] => Array
(
[leadData] => Array
(
[LeadID] => 1230279
[DateIdentified] => 19/04/2016
[Client] => Bank1
[LeadName] => test 3
[Owner] => Some Owner
[Value] => 36000.00
[Status] => 50%
)
[clientData] => Array
(
[BusinessStructure] =>
[IsProspect] => No
)
[quoteData] => Array
(
[QuoteID] => Q0016
[ProjectName] => test 3
[Amount] => 30000.00
[AmountTax] => 6000.00
[AmountIncludingTax] => 36000.00
[EstimatedCost] => 0.00
[EstimatedCostTax] => 0.00
[EstimatedCostIncludingTax] => 0.00
)
)
)
One thing to note is that sometimes not all of the information is there, simply because it is not available. So you can see that Array 0 has some customData whereas Array 1 does not. Other elements may not even have a quoteData section etc.
So my blade template now has this information. I basically want to show all the data, but if it is not available, to just show an empty cell. So I have the following headers
<table class="table table-bordered table-hover additionalMargin alignment">
<tr class="col-md-12 noPadding">
<thead>
<tr>
<th>Lead ID</th>
<th>Client Name</th>
<th>Is Prospect</th>
<th>Business Structure</th>
<th>Quote ID</th>
<th>Project Name</th>
<th>Amount</th>
<th>Amount Tax</th>
<th>Amount inc Tax</th>
<th>Cost</th>
<th>Cost Tax</th>
<th>Cost inc Tax</th>
<th>Type</th>
<th>Month</th>
</tr>
</thead>
</tr>
<tbody>
</tbody>
</table>
However, within the tbody, what I am doing seems very messy, the only way I can seem to get data in the correct place is if I check everything e.g.
#if(is_array($leadArray))
#foreach($leadArray as $array)
<tr>
<td>
#if(!empty($array['leadData']))
{{ $array['leadData']['LeadID'] }}
#endif
</td>
<td>
#if(!empty($array['leadData']))
{{ $array['leadData']['Client'] }}
#endif
</td>
<td>
#if(!empty($array['clientData']))
{{ $array['clientData']['IsProspect'] }}
#endif
</td>
<td>
#if(!empty($array['clientData']))
{{ $array['clientData']['BusinessStructure'] }}
#endif
</td>
<td>
#if(!empty($array['quoteData']))
{{ $array['quoteData']['QuoteID'] }}
#endif
</td>
<td>
#if(!empty($array['quoteData']))
{{ $array['quoteData']['ProjectName'] }}
#endif
</td>
<td>
#if(!empty($array['quoteData']))
{{ $array['quoteData']['Amount'] }}
#endif
</td>
<td>
#if(!empty($array['quoteData']))
{{ $array['quoteData']['AmountTax'] }}
#endif
</td>
<td>
#if(!empty($array['quoteData']))
{{ $array['quoteData']['AmountIncludingTax'] }}
#endif
</td>
<td>
#if(!empty($array['quoteData']))
{{ $array['quoteData']['EstimatedCost'] }}
#endif
</td>
<td>
#if(!empty($array['quoteData']))
{{ $array['quoteData']['EstimatedCostTax'] }}
#endif
</td>
<td>
#if(!empty($array['quoteData']))
{{ $array['quoteData']['EstimatedCostIncludingTax'] }}
#endif
</td>
#if(!empty($array['customData']))
#foreach($array['customData'] as $data)
<td>
#if(!empty($data['Lead Type']))
{{ $data['Lead Type'] }}
#endif
</td>
<td>
#if(!empty($data['Month']))
{{ $data['Month'] }}
#endif
</td>
#endforeach
#else
<td></td>
<td></td>
#endif
</tr>
#endforeach
#endif
Is there a neater way to do this? Or is this my only option?
Thanks

Blade has a feature where you can use or to mean "echo this if it exists, or this if it doesn't". So you can do
<td>{{ $array['leadData']['LeadID'] or '' }}</td>
And that basically results in what you want. Much cleaner, no? :)
Documentation: https://laravel.com/docs/5.2/blade#displaying-data - a few paragraphs down.

You also could do this more elegantly and shorter by iterating array with column names (used or '' as Joel advised):
{{ $dataColumns['leadData'] = ['LeadID', 'Client'] }}
{{ $dataColumns['clientData'] = ['IsProspect', 'BusinessStructure'] }}
{{ #dataColumns['quoteData'] = ['QuoteID', 'ProjectName', 'Amount', 'AmountTax', 'AmountIncludingTax', 'EstimatedCost', 'EstimatedCostTax', 'EstimatedCostIncludingTax'] }}
#if(is_array($leadArray))
#foreach($leadArray as $array)
<tr>
#foreach($dataColumns as $dataColumn)
#foreach($dataColumn as $column)
<td>{{ $array[$dataColumn][$column] or '' }}</td>
#endforeach
#endforeach
#if(!empty($array['customData']))
#foreach($array['customData'] as $data)
<td>{{ $data['Lead Type'] or ''}}</td>
<td>{{ $data['Month'] or '' }}</td>
#endforeach
#else
<td></td>
<td></td>
#endif
</tr>
#endforeach
#endif

Related

How to access map function in Laravel

This is my controller code:
public function RentCertificate()
{
$report = Report::distric()->status(1)->desk(15)->get()
->groupBy(function (Report $item) {
return $item->created_at->format('Y-m');
})
->map(function($rows, $key) {
return [
'column_one' => $rows->column_one,
'rows' => $rows
];
});
return view('adcr.report.rent_certificate', compact('report'));
}
when I try
#foreach($report as $key => $data)
<tr role="row" class="odd">
<td class="input_bangla">{{ $key+1 }}</td>
<td>{{ $data->fiscal_year }}</td>
<td>{{ $data->month }}</td>
</tr>
#endforeach
I got:
Property [fiscal_year] does not exist on this collection instance.
How can I access this data?
Here,
#foreach($report as $key => $data)
$data is the array you've mapped. An array like ['column_one' => ..., 'rows' => ...], it's not an object. You need to access one of its keys (column_one or rows).
Also, you can use a ternary operator with loop iteration to assign the classes even or odd in your <tr> element.
#foreach($report as $key => $data)
<tr role="row" class="{{ ($loop->iteration % 2) ? 'even' : 'odd' }}">
<td class="input_bangla">{{ $key+1 }}</td>
<td>{{ $data['rows']->fiscal_year }}</td>
<td>{{ $data['rows']->month }}</td>
</tr>
#endforeach

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 display nested data in table laravel

How can I get the result as drawn on the right
in Controller
$programs = Program::with(['activities', 'activites.subactivities'])->get();
in Blade View
#foreach ($programs as $programKey => $program)
#foreach ($program->activities as $activity)
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>{{ $programKey + 1 }}</td>
<td>{{ strtoupper($program->name) }}</td>
<td>{{ $activity->name }}</td>
<td>{{ $subactivity['name'] }}</td>
</tr>
#endforeach
#endforeach
#endforeach
I would say to save in a variable and if different the show
Like this:
#foreach ($programs as $programKey => $program)
#php
$newProgram = true;
#endphp
#foreach ($program->activities as $activityKey => $activity)
#php
$newActivity = true;
#endphp
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>
#if($newProgram == true)
{{ $programKey + 1 }}
#endif
</td>
<td>
#if($newProgram == true)
{{ strtoupper($program->name) }}
#php
$newProgram = false;
#endphp
#endif
</td>
<td>
#if($newActivity == true)
{{ $activity->name }}
#php
$newActivity = false;
#endphp
#endif
</td>
<td>
{{ $subactivity['name'] }}
</td>
</tr>
#endforeach
#endforeach
#endforeach

How do I calculate the sub total and total in a table in the laravel?

If I debug my laravel eloquent, the result like this :
SELECT a.transaction_number a.date, a.item_number, b.desc, a.variant_code, sum(a.quantity) AS quantity, a.cost
FROM `items_details` AS a
JOIN `items` AS b ON b.id = a.item_number
WHERE a.item_number = 0101010
GROUP BY a.variant_code
ORDER BY transaction_number, variant_code
I will return collection with pagination
But here I just display the array. The array like this :
$data = array(
array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '002', 'quantity' => '2','cost' => '2000'),
array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '004', 'quantity' => '3','cost' => '2000'),
array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '005', 'quantity' => '4','cost' => '2000'),
array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '006', 'quantity' => '5','cost' => '2000'),
array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '008', 'quantity' => '1','cost' => '2000'),
array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '013', 'quantity' => '2','cost' => '2000'),
array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '020', 'quantity' => '3','cost' => '2500'),
array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '022', 'quantity' => '4','cost' => '2500'),
array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '007', 'quantity' => '1','cost' => '2500'),
array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '015', 'quantity' => '7','cost' => '2500')
);
My script in the laravel blade to display it like this :
<table class="table">
<tr>
<th>transaction_number</th>
<th>date</th>
<th>item_number</th>
<th>desc</th>
<th>variant_code</th>
<th>quantity</th>
<th>cost</th>
</tr>
#foreach ($items as $item)
<tr>
<td>{{ $item->transaction_number }}</td>
<td>{{ $item->date }}</td>
<td>{{ $item->item_number }}</td>
<td>{{ $item->desc }}</td>
<td>{{ $item->variant_code }}</td>
<td>{{ $item->quantity }}</td>
<td>{{ $item->cost }}</td>
</tr>
#endforeach
</table>
The result like this :
I want the result like this :
What is the best way to do it? Whether through mysql query or laravel eloquent to display like that? Or is it arranged through the view blade laravel?
Please, help me. I'm confused :)
Without pagination, I would do something like this (not tested)...
Requiring pagination adds complexity with regards to potentially breaking up transaction groups and the total.
Have a read of the following,
https://laravel.com/docs/5.6/blade#the-loop-variable
https://laravel.com/docs/5.6/blade#including-sub-views
You need Laravel 5.3+ for $loop.
subtotal.blade.php
<tr>
<td colspan="4"></td>
<td>SUBTOTAL</td>
<td>{{ $items->where('transaction_number', $current_transaction_number)->sum('quantity') }}</td>
<td>{{ $items->where('transaction_number', $current_transaction_number)->sum('cost') }}</td>
</tr>
<tr>
<td colspan="7"></td>
</tr>
total.blade.php
<tr>
<td colspan="4"></td>
<td>TOTAL</td>
<td>{{ $items->sum('quantity') }}</td>
<td>{{ $items->sum('cost') }}</td>
</tr>
main.blade.php
<table class="table">
<tr>
<th>transaction_number</th>
<th>date</th>
<th>item_number</th>
<th>desc</th>
<th>variant_code</th>
<th>quantity</th>
<th>cost</th>
</tr>
#php ($current_transaction_number = null)
#foreach ($items as $item)
#if ($loop->index > 0 && $current_transaction_number != $item->transaction_number)
#include ('subtotal', compact('items', 'current_transaction_number'))
#endif
<tr>
#if ($current_transaction_number == $item->transaction_number)
<td colspan="2"></td>
#else
#php ($current_transaction_number = $item->transaction_number)
<td>{{ $item->transaction_number }}</td>
<td>{{ $item->date }}</td>
#endif
<td>{{ $item->item_number }}</td>
<td>{{ $item->desc }}</td>
<td>{{ $item->variant_code }}</td>
<td>{{ $item->quantity }}</td>
<td>{{ $item->cost }}</td>
</tr>
#if ($loop->last)
#include ('subtotal', compact('items', 'current_transaction_number'))
#include ('total', compact('items'))
#endif
#endforeach
</table>
{{ $items->links() }}
#php
$previous_transaction_number = "";
$total_quantity = 0;
$total_cost = 0;
$subtotal_quantity = 0;
$subtotal_cost = 0;
#endphp
<table class="table" border='1'>
<tr>
<th>transaction_number</th>
<th>date</th>
<th>item_number</th>
<th>desc</th>
<th>variant_code</th>
<th>quantity</th>
<th>cost</th>
</tr>
#foreach ($items as $item)
#if(!empty($previous_transaction_number) && $previous_transaction_number != $item['transaction_number'])
<tr>
<td colspan="5" style="text-align:right"><b>SUBTOTAL:</b></td>
<td><b> {{ $subtotal_quantity }} </b></td>
<td><b> {{ $subtotal_cost }} </b></td>
</tr>
#php
$subtotal_quantity = 0;
$subtotal_cost = 0;
$previous_transaction_number = "";
#endphp
<tr>
<td colspan="7"> </td>
</tr>
#endif
<tr>
#if($previous_transaction_number != $item['transaction_number'])
<td>{{ $item['transaction_number'] }}</td>
#else
<td></td>
#endif
<td>{{ $item['date'] }}</td>
<td>{{ $item['item_number'] }}</td>
<td>{{ $item['desc'] }}</td>
<td>{{ $item['variant_code'] }}</td>
<td>{{ $item['quantity'] }}</td>
<td>{{ $item['cost'] }}</td>
</tr>
#php
$subtotal_quantity += $item['quantity'];
$subtotal_cost += $item['cost'];
$previous_transaction_number = $item['transaction_number'];
$total_quantity += $item['quantity'];
$total_cost += $item['cost'];
#endphp
#endforeach
<tr>
<td colspan="5" style="text-align:right"><b>SUBTOTAL:</b></td>
<td><b> {{ $subtotal_quantity }} </b></td>
<td><b> {{ $subtotal_cost }} </b></td>
</tr>
<tr>
<td colspan="7"> </td>
</tr>
<tr>
<td colspan="5" style="text-align:right"><b>TOTAL:</b></td>
<td><b> {{ $total_quantity }} </b></td>
<td><b> {{ $total_cost }} </b></td>
</tr>
</table>

How to hide variable in laravel

I have a code like below
#foreach ($model as $rows)
<tr>
<td>{{ $i++}}</td>
<td>
{{
Employee::where('nik','=',$rows->nik)->first(['nama'])
}}
</td>
<td>{{ $rows->nik }}</td>
<td>
{!! HTML::link('employee/profile_app/'.$rows->nik,'view', array('class'=>'fa fa-pencil-square-o action-button')) !!}
</td>
</tr>
#endforeach
and the result is like this
enter image description here
How to hide that "nama" variable
#foreach ($model as $rows)
<tr>
<td>{{ $i++}}</td>
<td>
{{
Employee::where('nik','=',$rows->nik)->first(['nama'])->nama
}}
</td>
<td>{{ $rows->nik }}</td>
<td>
{!! HTML::link('employee/profile_app/'.$rows->nik,'view', array('class'=>'fa fa-pencil-square-o action-button')) !!}
</td>
</tr>
#endforeach

Resources