how to show combine Array in Datatable - laravel

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

Related

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

Laravel year, sum, count, group by eloquent query

Can someone show me how I would build this query with eloquent, or is this required to be written with a DB:Raw query, or would it be best to be a raw sql query?
select `pay_week`,
year(`start_timestamp`) as year,
sum(`total_duration`) as 'duration',
count(`pay_week`) as 'entries'
from `time_clock`
group by year(`start_timestamp`),`pay_week`;
Any help to better understand would be greatly appreciated.
Thank you.
$entries = TimeClock::select(
'pay_week',
DB::raw('count(pay_week) as entries'),
DB::raw('YEAR(start_timestamp) as year'),
DB::raw('sum(total_duration) as duration')
)
->where('user_id', $user->record_id)
->sum('total_duration')
->groupBy('pay_week')
->get();
results in a
Call to a member function groupBy() on float
What I have now;
Blade;
<table class="table table-hover dataTable table-striped width-full" data-plugin="dataTable">
<thead>
<th>Pay Week</th>
<th>Date Range</th>
<th>Year</th>
<th>Total Hours</th>
<th>Gross Pay</th>
<th>Entries</th>
<th>Action</th>
</thead>
<tbody>
#foreach($entries as $group)
#foreach($group->entries as $entry)
#php
$week_start = (new DateTime())->setISODate(date("Y"),$entry->pay_week)->format("m/d");
$start = \Carbon\Carbon::createFromFormat("m/d", $week_start);
$end = $start->copy()->endOfWeek()->format('m/d');
$year = \Carbon\Carbon::parse($entry->start_timestamp)->format('Y');
#endphp
<tr>
<td>{{ $entry->pay_week }}</td>
<td>{{ $start->format('m/d') . ' - ' . $end }}</td>
<td>{{ $year }}</td>
<td>0</td>
<td>$ 0.00</td>
<td>0</td>
<td>[ Btn ]</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
This is what I have in the controller method.
$user = Auth::guard('user')->user();
$entries = TimeClock::select(
'pay_week',
'start_timestamp',
'total_duration'
)
->where('user_id', $user->record_id)
->get()
->groupBy('pay_week')
->map(function($entry) {
return (object)[
'count' => $entry->count(),
'duration' => $entry->sum('total_duration'),
//'year' => $entry->sum('start_timestamp')->year,
'entries' => $entry
];
});
return view('user.timeclock.reports.all', [
'entries' => $entries
]);
Given, that you set the cast in your model
class TimeClock extends Model
{
protected $cast = [
'start_timestamp' => 'datetime'
];
}
you can do the following:
$entries = TimeClock::select(
'pay_week',
'start_timestamp',
'total_duration'
)
->where('user_id', $user->record_id)
//->groupBy('pay_week')
->get()
->groupBy([ 'pay_week', function($entry) {
return $entry->start_timestamp->year
})
/*->map(function($entry) {
return (object)[
'count' => $entry->count(),
'duration' => $entry->sum('total_duration'),
'year' => $entry->start_timestamp->year,
'entries' => $entry
];
})*/;
You loop over it like this:
<table>
<thead>
<tr>
<th>Pay week</th>
<th>Date range</th>
<th>Year</th>
<th>Total hours</th>
</tr>
</thead>
<tbody>
#foreach($entries as $pay_week => $groups)
#foreach($groups as $year => $group)
<!--pay_week: {{ $pay_week }}<br />
count: {{ $group->count() }}<br />
duration: {{ $group->sum('total_duration') }}<br />
year: {{ $year }}<br />-->
<tr>
<td>{{ $pay_week }}</td>
<td></td>
<td>{{ $year }}</td>
<td>{{ $group->sum('total_duration') }}</td>
</tr>
#foreach($group as $entry)
#endforeach
#endforeach
#endforeach
</tbody>
</table>

Laravel display withCount() query results in groups

I have a query where I get count totals using withCount().
$applications = Application::with('company')->withCount([
'task',
'task as task_updated_count' => function ($query) {
$query->where('task_status', '!=', 1);
}])->get();
In my view I can loop through and display the results.
<table class="table">
<thead>
<tr>
<th>Applications</th>
<th class="text-center">Tasks</th>
<th class="text-center">Updated Tasks</th>
</tr>
</thead>
<tbody>
#foreach ($applications as $application)
<tr>
<td>{{ $application->company->company_acronym }}</td>
<td>{{ $application->application_name }}</td>
<td class="text-center">{{ $application->task_count }}</td>
<td class="text-center">{{ $application->task_updated_count }}</td>
</tr>
#endforeach
</tbody>
</table>
This will obviously give me a table listing the results with the $application->company->company_acronym listed for each $application.
What I'm trying to do list the applications by company "$application->company->company_acronym".
So the result would be:
Company 1 Acronym
- AppName Count
- AppName Count
- AppName Count
Company 2 Acronym
- AppName Count
- AppName Count
- AppName Count
Company 3 Acronym
- AppName Count
- AppName Count
- AppName Count
Use groupBy function in collections to group by the acronym.
$applications = Application::with('company')->withCount([
'task',
'task as task_updated_count' => function ($query) {
$query->where('task_status', '!=', 1);
}])->get();
$applicationGroups = $applications->groupBy(function($application) {
return $application->company->company_acronym;
});
Then you can iterate over the groups to get desired output.
<table class="table">
<thead>
<tr>
<th>Applications</th>
<th class="text-center">Tasks</th>
<th class="text-center">Updated Tasks</th>
</tr>
</thead>
<tbody>
#foreach ($applicationGroups as $group => $applications)
<tr>
<td colspan="3">{{ $group }}</td>
</tr>
#foreach ($applications as $application)
<tr>
<td>{{ $application->application_name }}</td>
<td class="text-center">{{ $application->task_count }}</td>
<td class="text-center">{{ $application->task_updated_count }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
Please Check Following Code
$applications = Application::with('company')->with([
'task' => function ($query) {
$query->where('task_status', '!=', 1)->count();
},
'task_updated_count' => function ($query) {
$query->where('task_status', '!=', 1)->count();
}])->get();

properties name of an Laravel Object

I have this Object in Laravel:
array:144 [▼
0 => {#10 ▼
+"year": 2005
+"month": "ene"
+"FOO": "37"
+"BAR": "17"
}
1 => {#11 etc...
I need to put the names "year", "month", "FOO" and "BAR" as in a table:
<thead>
<tr>
#foreach($data as $column_name)
<th><strong>{{ $column_name }}</strong></th>
#endforeach
</tr>
</thead>
$data is the Object.
So the table should looke like:
year | month | FOO | BAR
------------------------
2005 ene 37 17
The values are working fine, but I don't know how to retrieve the name of the properties to build the
EDIT:
This Is what I get according to some answers:
If $data is the first stdClass object in the array, then you should be able to do the following:
<thead>
<tr>
#foreach((array) $data[0] as $key => $value)
<th><strong>{{ $key }}</strong></th>
#endforeach
</tr>
</thead>
Try this:
<thead>
<tr>
#foreach($data as $key)
<th><strong>{{ $key['year'] }}</strong></th>
#endforeach
</tr>
</thead>
Update:
<thead>
<tr>
#foreach($data as $key)
<th><strong>{{ $key->year }}</strong></th>
#endforeach
</tr>
</thead>

Laravel Blade - Displaying array content

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

Resources