add rows and columns base on condition in laravel blade - php

I'm trying to create UI for cinema seats in laravel blade. i have seat_rows and seat_coulmns in my database and seat_rows will be the same for 2 rows. i mean first row is like this: id:1 seat_row:1 seat_column:1 and second row is like this: id:2 seat_row:1 seat:column:2. so i using bootstrap row and col classes and i wanna add row class only when number of seat_row changes. how can I DO that?
#foreach ($allseats as $seat)
{{-- #while ($seat->seat_row !== $seat->seat_row) --}}
<div id="{{$seat->seat_row}}" class="row ">
{{-- #for ($i = 1; $i <= 2; $i++) --}}
<div id="{{$seat->id}}" class="col-2 seats ">
<img src="{{asset('assets/img/seats/seat1.png')}}" alt="" style="height:50px;width:50px">
</div>
{{-- #endfor --}}
</div>
{{-- #endwhile --}}
{{$newrow=$seat->seat_row}}
#endforeach

you need to compare it with the previous seat.
Instead of using #foreach ($allseats as $seat) you can use #foreach ($allseats as $key => $seat)
and then check in your loop if $seat !== $allseats[$key-1]
I hope that guides you on the correct track.

Related

Laravel Tailwind CSS - Tooltip didn't get data, but parent div gets it

I've got a table with attendance, where I just pass specific color to div and id of attendance, but I didn't understand one thing: The id passed like <div id={{ attendance->id }}></div> works, but when I pass other data just one line lower like I did before it gets the latest even if I empty that var after use?
#for ($i = 1; $i <= 30; $i++ )
#foreach ($user->attendances as $attendance)
#php
$temp = strtotime(Carbon\Carbon::parse(Carbon\Carbon::now())->startOfMonth()->setMonth(Carbon\Carbon::now()->month)->setDay($i)->format('Y-m-d'));
$start = strtotime(Carbon\Carbon::parse($attendance->start));
$end = strtotime(Carbon\Carbon::parse($attendance->end));
#endphp
#if ($temp >= $start && $temp <= $end)
#php
$user->cell = $attendance->id;
$user->style = $attendance->reasons->color;
#endphp
#endif
#endforeach
<td class="border border-slate-600">
<div data-tooltip-target="tooltip-default" class="rounded-md {{ $user->style }} py-2 px-3" style="background-color: {{ $user->style }}" id="{{ $user->cell }}"> <!-- Here it gives right value to each div -->
<div id="tooltip-default" role="tooltip" class="inline-block absolute invisible z-10 py-2 px-3 text-sm font-medium text-white bg-gray-900 rounded-lg shadow-sm opacity-0 transition-opacity duration-300 tooltip dark:bg-gray-700">
ID attendance:{{ $user->cell }}<br> <!-- it gives the latest ID in DB. -->
<div class="tooltip-arrow" data-popper-arrow></div>
</div>
</div>
</td>
#php
$user->cell = '';
$user->style = '';
#endphp
#endfor
How to explain that? Is there better way to create tooltip to each div in table cell?
Update:
Overriding stylizations can cause be a pain. However, in the OP situation injecting a hex rgb value was not able to be injected in the class attribute.

foreach in foreach in if blade php

#foreach ($capacityGroups as $capacityGroupFather)
#foreach ($exampleProjects as $k => $exampleProject)
#foreach ($exampleProject->capacityGroups as $j => $exampleCapacity)
#if ($exampleCapacity->id == $capacityGroupFather->id)
Only the first result of the loop
<div class="carousel-item active">
The rest of the loop
<div class="carousel-item">
We have the example Projects collection, each of which has capacityGroups.
I need to bring all the example Projects for each capacity Group. But the first of each goes into a special div (carousel-item active)
and the rest of each goes into (carousel-item) how can I fix this problem
Simply use the loop variable.
#foreach ($exampleProjects as $k => $exampleProject)
<div class="carousel-item{{ $loop->first ? ' active' : '' }}">
</div>
#endforeach

setting variables inside a Laravel foreach loop

I've just started using the framework. In plain PHP after the opening foreach I would then set the variables then close the php tag but then from what I can work out you have to then do the Laravel #foreach tags and then open and close #php. Is there a way around this as it seems like a lot of extra work and code?
#foreach($steps as $row)
#php
$title = $row->title;
$text = $row->text;
$i = 1;
#endphp
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{$title}}</h2>
#endif
{!! $text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
Since blade is no PHP, you have to return to PHP with that directive. But you can set/use the variables without doing that in your case:
#foreach($steps as $i => $row)
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{ $row->title }}</h2>
#endif
{!! $row->text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
If you still want to set variables, there's a Laravel package called alexdover/blade-set. But as #brombeer pointed out, in most cases it's highly recommended to set all necessary variables in the controller before passing them to the view.
Use laravel provided loop variables:
$loop->iteration The current loop iteration (starts at 1).
It will increment in every loop iteration automatically.
e.g:
First iteration = $loop->iteration => 1 ;
Second iteration = $loop->iteration => 2 ;
so on until loop ends.
Check docs:
The Loop Variables
You can use a #for directive with sizeof($steps) like that:
#for($i=0; $i<= sizeof($steps)-1; $i++)
#endfor
#foreach ($steps as $row)
<div class="steps-item grid-wrap">
<div class="number">
<div class="text-wrap">
#if ($row->title != '')
<h2>{{$row->title}}</h2>
/* if you want to display another title when its blank you can
use if-else here otherwise not need to use if conditions for
title and text */
#endif
#if ($row->text != '')
{!! $row->text !!}
#endif
</div>
</div>
</div>
#endforeach
{{--
for an example you have $steps values like
$steps =
Array(
[0] -> 1,
[1] -> 'title',
[2] -> 'text'
);
if you want this array key value pair you have to use #foreach like
#foreach ($steps as $key=>$value)
#endforeach
you can use key value in #foreach loop only
--}}

Display text on each iteration laravel Livewire

I need your help. Lets say I have array with 10 elements and every record to be shown on every iteration and I want to be done with Livewire, let me share with you some code and will tell you what I have tried till now.
public $content;
public $array = ['first', 'second', 'third' ,'fourth'];
foreach ($array as $item) {
sleep(1);
$this->content[] = "Element ${item}";
}
<div class="modal-body">
#if ($content)
<ul class="listree">
#foreach ($content as $element)
<li>
<div class="listree-submenu-heading">
{!! $element['title'] !!}
</div>
<ul class="listree-submenu-items">
#foreach ($element['elements'] as $sub)
<li>
<div class="listree-submenu-heading">
{!! $sub['title'] !!}
</div>
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>
#endif
</div>
My idea is display first record, wait 1 second, display first and second record, wait 1 second, display first, second and third records and so on... How I can do this with livewire. The problem is that $content is filled with the information after all iteration and then the component is refreshed.
I tried on every iteration to send custom event which will call refresh method, but without succes. I will appreaciate any advice, and if you need more information, I will provide it.
Assumming you're also using alpinejs, this can be done pretty easily.
<x-app-layout>
<div class="text-gray-800 ml-10">
<ul class="bg-green-200">
<!-- The main foreach loop. -->
#foreach (range('a', 'z') as $element)
<!-- render al <li> tags with display:none. -->
<!-- Show the 1st after 0s, the 2nd after 1s, the 3rd after 2s, ... -->
<li class="bg-blue-200 m-5"
x-data="{show: false, index: {{ $loop->index }} }"
x-init="setTimeout(() => show = true, index * 1000)">
<div x-show="show">
{{ $element }}
...
</div>
</li>
#endforeach
</ul>
</div>
</x-app-layout>
$loop is a special object you can access within a #foreach block.

foreach with every 10th and 2nd changes

So i am dealing with coupons that I need to print. I prepared the view and I am passing coupons to the view:
public function printSelected($ids){
$couponIDs = explode(',', $ids);
$selectedCoupons = Coupon::find($couponIDs);
return view('admin.coupons.print')->with(compact('selectedCoupons'));
}
Now I need to loop through them in a certain manner.
every 10 coupons I need a new "page" block because 10 coupons fit into a page block
every second coupon I need a new table row because 2 coupons or table data fits into one row
There are multiple ways to do this but I find it very readable to use the chunk method of the Collection object. Another option would be to use modulus ($index % 10 === 0).
<div class="coupons">
{{-- Create chunks of 10 as this is the max per page --}}
#foreach($selectedCoupons->chunk(10) as $pageCoupons)
<div class="page">
<div class="subpage">
<table>
{{-- Create chunks of 2 so every row is filled with up to 2 coupons --}}
#foreach($pageCoupons->chunk(2) as $rowCoupons)
<tr>
{{-- Loop over the row coupons to create the columns --}}
#foreach($rowCoupons as $coupon)
<td>
<span class="coupon">{{ $coupon->code }}</span><br>
<hr>
<span class="name">{{ $coupon->user->name }}</span>
</td>
#endforeach
</tr>
#endforeach
</table>
</div>
</div>
#endforeach
</div>
You can use the collection's chunk method to loop over chunks of your coupons:
<div class="coupons">
#foreach ($selectedCoupons->chunk(10) as $page)
<div class="page">
<div class="subpage">
<table>
#foreach ($page->chunk(2) as $row)
<tr>
#foreach ($row as $coupon)
<td>
<span class="coupon">{{ $coupon->code }}</span><br>
<hr>
<span class="name">{{ $coupon->user->name }}</span>
</td>
#endforeach
</tr>
#endforeach
</table>
</div>
</div>
#endforeach
</div>

Categories