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>
Related
Hello I have these columns 'name', 'email', 'username' and 'ip' how would I check or highlight table row in blade if records has same ip?
This is my controller
$promo = Promo::with(['participants' => function ($q) {
$q->orderBy('winner', 'desc')->orderBy('created_at', 'desc');
}])->find($id);
And this is my blade
#if($promos->participants->count() > 0)
#foreach($promos->participants as $participant)
<table>
<tr class="align-middle">
<td>Name</td>
<td>Email</td>
<td>Username</td>
<td>IP</td>
</tr>
<tr>
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
</table>
#endforeach
#endforeach
Another way is to get duplicates by taking advantage of Larvel's collection and have them passed in view.
$ips = $promos->participants->pluck('ip');
$duplicateIps = $ips->duplicates()->unique()->all();
So in your blade you would just need to check
#if($promos->participants->isNotEmpty())
#foreach($promos->participants as $participant)
<table>
<tr class="align-middle">
<td>Name</td>
<td>Email</td>
<td>Username</td>
<td>IP</td>
</tr>
<tr #class(['duplicate-row' => in_array($participant->ip, $duplicateIps)]>
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
</table>
#endforeach
Do like this
$ipList will store IPs when foreach runs and keeps checking whether they exist. If yes, add class duplicate-row to the <tr>
$ipList = [];
#foreach($promos->participants as $participant)
<tr class="align-middle #if(in_array($participant->ip, $ipList)) duplicate-row #endif">
<td>{{$participant->name}}</td>
<td>{{$participant->email}}</td>
<td>{{$participant->username}}</td>
<td>{{$participant->ip}}</td>
</tr>
#if(!in_array($participant->ip, $ipList))
<?php array_push($ipList, $participant->ip); ?>
#endif
#endforeach
In CSS you can add
.duplicate-row {
background-color: red;
}
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
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
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();
I have a Function to showing a single data . but if this data empty its having error. I using first because just want to
Trying to get property of non-object
its my Controller
public function PDF_profile(Request $request,$id){
$users = User::findOrFail($id);
$pns = Data_pns::where('user_id',$id)->first();
$pendidikan = Data_riwayat_pendidikan::where('user_id',$id)->latest()->first();;
$r_kgb = DB::table('data_riwayat_kgb')->latest()->first();
$pdf = PDF::loadView('admin.pdf_profile',
['users' => $users,
'pendidikan'=>$pendidikan,
'r_kgb'=>$r_kgb,
'pns' => $pns,
]);
// dd($pns);
return $pdf->stream('Profile.pdf')->header('Content-Type','application/pdf');
}
my View
<table align="left" border="" >
<thead>
<tr>
<th> <b>Riwayat Pendidikan </b></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td> <b>Tingkat Pendidikan </b></td>
<td>:
{{ $pendidikan->jenjang}}
</td>
</tr>
<tr>
<td> <b> Tempat Pendidikan</b></td>
<td>:{{$pendidikan->nama_tempat}}</td>
</tr>
<tr>
<td><b>Konsentrasi/Jurusan</b></td>
<td>:{{ $pendidikan->jurusan}}</td>
</tr>
<tr>
<td><b>Tahun Lulus</b></td>
<td>:{{$pendidikan->lulus_tahun}}</td>
</tr>
</tbody>
</table>
its just part on my view .
and this line is mentioned error .
<td>:
<?php echo e($pendidikan->jenjang); ?>
</td>
but if this data is inputed or not empty its not having error . how to solved it ?
You can first check if the object is not null as first() returns null hence the error, so do this instead:
<td>:
#if($pendidikan)
{{$pendidikan->nama_tempat}}
#endif
</td>
You can also replace this: ->latest()->first() with just calling last().
You can use the optional() helper method
{{ optional($pendidikan)->nama_tempat}}
or short form #
{{ #$pendidikan->nama_tempat}}