I am trying to output a multidimensional array in my view, but am getting some wacky output on the table. Here is a screen grab of what I have:
Here is the code I am using:
</tr>
#endforeach
</tbody>
</table>
I think it has something to do with my if else statement, but have been stuck on this problem for some time now. Any help would be much appreciated! Thanks
EDIT: As requested, here is my data structure and it's var_dump:
It is hard to tell without your data structure, but I imagine the issue is with your data structure and your nested foreach.
When you are checking the $status you loop through the array. The first time you loop through the array $status == 1, but a td is being written every time you check the status.
#foreach ($count as $status => $c)
#if($status == 2)
<td>{{$c}}</td>
#else
<td> </td>
#endif
...
#endforeach
So on the first pass assuming $status = 1; You will get
<td> </td>
<td>{{$c}}</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
on the second pass, assuming $status = 2 you will get:
<td>{{$c}}</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
but you are still on the first person.
What you might consider is changing the structure of your data to something like this. This is psuedo code so please modify as necessary.
$person = [
$totals => [
$qualifiedProspectCount => 2,
$unqualifiedProspectCount => 2,
$contactedCount => 2,
$qualifiedProspectCount => 2,
$convertedToAccountCount => 2,
$pendingIntegrationCount => 2,
$revenueGeneratingCountCount => 2
]
];
Then only gling through your data once like this. I extracted the into a partial, but you could continue to do it inline.
page.blade.php
<table class="table tablesorter" id="tblAffs">
<thead>
<tr class="tblhead">
<th>AM</th>
<th>Qualified Prospect</th>
<th>Unqualified Prospect</th>
<th>Contacted</th>
<th>Converted To Account</th>
<th>Pending Integration</th>
<th>Revenue Generating</th>
</tr>
</thead>
<tbody>
$totalCount should be a collection of $person
#foreach ($totalCount as $name => $totals)
<tr>
<td>
{{$name}}
<br>
Closed
</td>
#include('partial.item', 'count' => $totals->qualifiedProspectCount)
#include('partial.item', 'count' => $totals->unqualifiedProspectCount)
#include('partial.item', 'count' => $totals->contactedCount)
#include('partial.item', 'count' => $totals->convertedToAccountCount)
#include('partial.item', 'count' => $totals->pendingIntegrationCount)
#include('partial.item', 'count' => $totals->revenueGeneratingCountCount)
</tr>
#endforeach
</tbody>
partial/item.blade.php
#if($count > 0)
<td>{{{$count}}}</td>
#else
<td> </td>
#endif
EDIT
So this is going to get you into all kinds of trouble later on, but here you go.
#foreach ($totalCount as $name => $count)
<tr>
<td>
{{$name}}
<br>
Closed
</td>
#if(isset($count[2]))
<td>{{ $count[2] }}</td>
#else
<td> </td>
#endif
#if(isset($count[1]))
<td>{{ $count[1] }}</td>
#else
<td> </td>
#endif
// and so on ...
</tr>
#endforeach
I'd recommend checking out Laravel's Documentation, there are many ways to do what you are trying to do that are far easier than how you are going about it. Even though it feels faster. It isn't.
Related
from my codes under, i want to set foreach enter another foreach as shown, i have two tables (reservation and courses ), and the objective is to find the status of course who have the same ID in two tables.
Tables:
Courses: (id_course, statutCourses)
reservation: (id_reserv, Location, Destination, Date, Phone, price)
thanks for helping me
#foreach ($reservations as $reservation)
<tr>
<td> {{ $reservation->id_reserv}}</td>
<td> {{ $reservation->Location}}</td>
<td> {{ $reservation->Destination }}</td>
<td> {{ $reservation->Date }}</td>
<td> {{ $reservation->Phone }}</td>
<td> {{ $reservation->price }}</td>
<td>
#foreach($courses as $course)
#if($course->id_course == $reservation->id_reserv)
<table style="margin-bottom: 15px;">
<tr>
<td style="padding-right: 10px;">Statut :</td>
<td>{{$course->statutCourses}}</td>
</tr>
</table>
#endif
#endforeach
</td>
</tr>
#endforeach
so I recently learn how to use Laravel for my class project, and heres my problem
when I create pagination the result in my project is <
.1
>
like that, the pagination is not styled and the pagination like the picture I upload, I have done everything I could do , I browse the internet too, and the result is null , thanks heres the picture of my problem
$no = 1 {{-- buat nomor urut --}}
)
#if ($forms->count() > 0)
#foreach ($forms as $angs)
<tr align="center">
<td height="32">{{ $no++ }}<div align="center"></div></td>
<td>{{ $angs->username }}<div align="center"></div></td>
<td>{{ $angs->nama }}<div align="center"></div></td>
<td>{{ $angs->nik}}<div align="center"></div></td>
<td>{{ $angs->status_aktif}}<div align="center"></div></td>
<td bgcolor="#EEF2F7"><div align="center">Detail | Edit | Hapus</div></td>
<tr align="center" bgcolor="#DFE6EF">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tr>
#endforeach
{{ $forms->links()}}
`
this is my code in blade
and this is my code in my controller
`
$forms= login::paginate(3);
return view('crud.form.viewMember', compact('forms'));
`
I am using Laravel 5 and I have a page which have some tables. My problem is, when there is no data in database, it shows nothing. I want it show 'Data is not found'. This first picture below is when the data is available in the database, and the second picture is when the data is unavailable in the database.
1.
2.
This is my code:
#if($candidateskill != '')
#foreach($candidateskill as $getData)
<tr>
<td>{{ $getData->SKILL_NAME }}</td>
<td align="center">{{ $getData->SKILL_LEVEL }}</td>
</tr>
#endforeach
#else
<td>Data is Not Found</td>
<td>Data is Not Found</td>
#endif
And this is the output of the null data:
Illuminate\Database\Eloquent\Collection Object ( [items:protected] =>
Array ( ) )
You can use the #forelse (https://laravel.com/docs/5.6/blade#loops)
#forelse($candidateskill as $getData)
<tr>
<td>{{ $getData->SKILL_NAME }}</td>
<td align="center">{{ $getData->SKILL_LEVEL }}</td>
</tr>
#empty
<tr><td colspan="2">No Data</td></tr>
#endforelse
Try this:
#if($candidateskill && !$candidateskill->isEmpty())
#foreach($candidateskill as $getData)
<tr>
<td>{{ $getData->SKILL_NAME }}</td>
<td align="center">{{ $getData->SKILL_LEVEL }}</td>
</tr>
#endforeach
#else
<tr>
<td>Data is Not Found</td>
</tr>
#endif
You need to evaluate your database variable if it exists, and if its not empty aka null.
I need help with looping my product options, this is what i have now:
What I want is simply get 2 row's only one for color and another for size and in dropdown in front of each one have all items.
here is my blade code:
<tbody>
#foreach($product->suboptions as $option)
<tr>
<td style="width: 150px;">{{ $option->option->title }}</td>
<td class="text-left">
<select name="" id="">
<option value="{{$option->id}}">{{$option->title}} - {{ number_format($option->price, 0) }}</option>
</select>
</td>
</tr>
#endforeach
</tbody>
UPDATE
my dd result of {dd($product->suboptions)}}
You can first group them together with the mapToGroups() function for a collection
https://laravel.com/docs/5.5/collections#method-maptogroups
$something = $product->suboptions->mapToGroups(function ($item, $key) {
return [$item->option->title => $item];
});
You should dd() that to see what the output is and understand it.
After that you can just cycle through them with foreach, your blade should be something like
<tbody>
#foreach($something as $optiontitle => $optioncollection)
<tr>
<td style="width: 150px;">{{ $optiontitle }}</td>
<td class="text-left">
<select name="" id="">
#foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
For accomplishing that you need to get option first, and write a method to access options via products id or products object. so you can right something like this in products model:
public function get_option($product_id){
$sub_options = Product::whereId($product_id)->first()->suboptions()->get();
$option = array();
foreach($sub_options as $sub_option){
$option[] = $sub_option->options()->get();
}
return $option;
}
And then in view, you just call this method and put it in 2 foreach, one for option and the other for sub-option. like this code below:
<tbody>
#foreach($product->get_option($product->id) as $key=>$value)
<tr>
<td style="width: 150px;">{{ $value->title }}</td>
<td class="text-left">
<select name="" id="">
#foreach($value->suboptions() as $key=>$value2)
<option value="{{$value2->id}}">{{$value2->title}} - {{number_format($value2->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
I don't know whats your model methods but you can get the concept.
I Repeat again
you should not copy my code exactly. just follow along with the concept of what am I saying.
But this time according to your eloquent methods. this code should work.
Currently im using Laravel-Excel (MaatWebsite) and wanted to check whether excel data is same with data in database "furnitures" table
Controller
// assume $id is retrieve from parameter
$check_furnitures = Furniture::where('company_no', $id)->get();
$rows = Excel::load('storage\\app\\public\\upload\\furniture.xlsx', function($reader){$reader->noHeading();$reader->skipRows(1)})->get();
View
<table>
<thead>
<tr>
<td>Chair Name></td>
<td>Desk Name></td>
<td>Carpet Name></td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php $arr = array(); ?>
#foreach($rows as $key => $value)
#foreach($check_furnitures as $fur)
#if(
$fur->chairs == $value[1] &&
$fur->desks == $value[2] &&
$fur->carpets == $pvalue[3]
)
<?php $temp_array[] = $value[0] ?>
#endif
#endforeach
#endforeach
<tr>
<td>{{$value[1]}}</td>
<td>{{$value[2]}}</td>
<td>{{$value[3]}}</td>
<td>
#foreach($arr as $test)
#if($test == $value[0])
DUPLICATED VALUE
#endif
#endforeach
</td>
<tr>
<table>
Excel File
The most efficient way is to index your database results first instead of looping. Laravel has a $collection->keyBy('id') so that you can $collection->get($id) later.
Having said that, you can do it this way:
<table>
<thead>
<tr>
<td>Chair Name></td>
<td>Desk Name></td>
<td>Carpet Name></td>
<td>Status</td>
</tr>
</thead>
<tbody>
#php
$arr = array();
$check_furnitures->keyBy('number');
#endphp
#foreach ($rows as $key => $row)
<tr>
<td>{{ $row->chair }}</td>
<td>{{ $row->desk }}</td>
<td>{{ $row->table }}</td>
<td>
#if($check_furnitures->get($row->number) !== null)
DUPLICATED VALUE
#endif
</td>
</tr>
#endforeach
</tbody>
</table>