Loop through collection - target every item above a certain number - laravel

I'm looping through my collection to display all my data. Pretty standard stuff - nothing special.
I wan't to target and apply functionality to every element from number 9 onwards.
For example. The first 8 sessions are free, after that I want to append a button to the 9th+ ones to pay.
#foreach($sessions as $session)
<input type="text" name="event" value="{{ $counsellingSession->event_start_time }}" class="form-control datetimepicker" >
#endforeach
could i apply a class to all items in position 9 and above.

You could use the magic $loop variable Blade templates have in #foreach loops like this:
#foreach($sessions as $session)
#if($loop->index > 8)
<!-- Display payment button -->
#endif
#endforeach
Documentation: https://laravel.com/docs/master/blade#loops

you can do the following:
$count=0;
#foreach($sessions as $session)
$count++;
#if($count >=9)
here apply your classs
#else
<input type="text" name="event" value="{{ $counsellingSession->event_start_time }}" class="form-control datetimepicker" >
#endif
#endforeach

Simple solution for that is you can use the keys of array in loop,
#foreach($sessions as $key => $session)
#if($key > 8)
<input type="submit">
#endif
<input type="text" name="event" value="{{ $counsellingSession->event_start_time }}"
class="form-control datetimepicker" >
#endforeach

Related

need help logic if and loop laravel

I'm planning to do code confirmation in a program where it requires 2 or more codes and confirmation.I tried and stuck with code like this
#foreach ($kode as $k)
#if ($k->kode != $k->konfirmasi )
<form action="/tps/kode" method="post">
#csrf
<input type="text" name="{{ $k->status }}" id="{{ $k->status }}" placeholder="{{ $k->status }}">
<button type="submit">submit</button>
</form>
{{-- #break --}}
#else
succes
#endif
#endforeach
I tried to use break and continue but that's not the place, maybe. It seems more to the logic where if the konfirmasi is the same and everything is the same then it will be successful. but I'm confused what to use
If I understand your question correctly, then you should first validate the codes and after iterating over the $kode variable, check if a kode was not the same as the konfirmasi. If so, show the form.
#php($failure = false)
#foreach ($kode as $k)
#if ($k->kode != $k->konfirmasi )
#php
$failure = true;
$failedKode = $k;
#endphp
#endif
#endforeach
#if($failure)
<form action="/tps/kode" method="post">
#csrf
<input type="text" name="{{ $failedKode->status }}" id="{{ $failedKode->status }}" placeholder="{{ $failedKode->status }}">
<button type="submit">submit</button>
</form>
#endif
Although this is a solution, let me advise you, like #TimLewis mentioned, move this kind of logic to the controller. This logic doesn't belong in views.

Use a select option as variable further down the same form in laravel

I try to get the selected option for the Team select on my form to further use it in the same form to limit the available positions in the selected team.
I managed to limit each team, so that it has a maximum of 9 players.
The idea is to have +-2 players on each position( 2 forward, 2 center, 2 back, 3 substitutes) for each team, if the spots are full the 'position' (ex. forward) option doesn't show up for the selected team.
Might be helpful : I am using foreign keys : (player -> one position_id, team -> many players)
My positions table is consisted of id, name and max and it is passed as $positions
I am trying to figure out a way, i am new to the subject so if you guys can help me, would be a great.
Or tell me if it isn't possible.
Thanks in advance.
<form action="/player" method="POST" enctype="multipart/form-data" class="form-control">
#csrf
...
<div class="mb-3">
<select name="team_id">
<option selected>Select Team</option>
#foreach ($teams as $team)
#if ($team->players->count() < 9)
<option value="{{ $team->id }}">{{ $team->name }}</option>
#endif
#endforeach
</select>
</div>
<div class="mb-3">
<select name="position_id">
<option selected>Select Position</option>
#foreach ($teams as $team)
#if ($team->id == ???)
#foreach ($positions as $position)
#if ($position->count() > $position->max)
<option value="{{ $position->id }}" disabled>{{ $position->name }}</option>
#else
<option value="{{ $position->id }}">{{ $position->name }}</option>
#endif
#endforeach
#endif
#endforeach
</select>
</div>
...
<button type="submit" class="btn btn-primary">submit</button>
</form>
```

Checkbox selection with two foreach loops

I have below code for showing all taxes as checkboxes,
I am using same form for editing taxes,
#foreach($taxes as $t)
#foreach($grouptax_taxes as $tg)
<label class="checkbox">
<input type="checkbox" name="taxgroup_rate[]" {{ isset($taxgroup) && $t->tax_id == $tg->tax_id ? 'checked' : ''}} id="taxgroup_{{$t->tax_id}}" value="{{$t->tax_id}}">
<span></span>{{$t->tax_name}} - {{$t->tax_rate}} &percnt; </label>
#endforeach
#endforeach
Problem is that it repeats all taxes multiple times as grouptax_rate, I know it will do repeat, but not sure how to select only those checkboxes which values are in $grouptax_taxes and remaining $taxes should not be selected,
How can I do that?
For the checked if values are equal should be done inside the checkbox, i.e the foreach loop should only cover the checked
#foreach($taxes as $t)
<label class="checkbox">
<input type="checkbox" name="taxgroup_rate[]" value="{{$t->tax_id}}" id="taxgroup_{{$t->tax_id}}"
#foreach($grouptax_taxes as $tg)
{{ ($t->tax_id == $tg->tax_id)? 'checked' : ''}}
#endforeach >
<span>{{$t->tax_name}} - {{$t->tax_rate}}</span></label>
#endforeach

Laravel : Add text area if the user checked a specific choice (radio)

I want to display textarea or text input if the user select a specific choice in form
I tried to use if but even the choice (radio) doesn't display
view :
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
#if($value === 'else')
<input type="checkbox" id="{{ $value }}" name="{{ $question->id }}[answer]" value="{{ $value}}"/>
<input type="text" name="{{ $question->id }}[answer]" placeholder="Votre commentaire"/>
<label for="{{$value}}">{{ $value }}</label>
#else
<p style="margin:0px; padding:0px;">
<input type="checkbox" id="{{ $value }}" name="{{ $question->id }}[answer]" value="{{ $value}}"/>
<label for="{{$value}}">{{ $value }}</label>
</p>
#endif
#endforeach
The question's choice is array ,the table :
The else choice doesn't display in the page

Laravel, check if value is in some collection (in blade)

I have 2 collections, $all have all the values (5), and $some has only 2 values. I'm trying to this:
For each value of $all, if that item in the loop is in the $some collection, put the the value of that $some item (something like that) in the input:
#foreach($all as $item)
<div>
#if(in_array($item->id, $some))
<input type="number" value="{{ Here I need to put the value of $some where id of some be teh same of $all }}">
#else
<input type="number" value="0">
#endif
</div>
#endforeach
I don't know how to read and decide all that inside the blade.
EDIT: I was trying something like this:
#if(in_array($item->id, $some))
<input type="number" value="{{ $some->find($item->id)->value }}">
I don't know what the specific fields are on $some, but you can do what you want (replacing the fields you need to compare or show) like this:
#if(in_array($item->id, $some->pluck('id')->toArray()))
<input type="number" value="{{ $some->where('id', $item->id)->first()->fieldYouWant}}">
#else
<input type="number" value="0">
#endif
Basically, pull the $some ids into an array, compare the current looped $item->id (from $all), and if it hits, pull the specific $some object that matched (id from $some, and id from current $all item) and get the value you need from whatever the field you need is called.
In my case, i was trying to do this on a array of checkboxes input, in case someone needs it this way:
<input type="checkbox" name="guides[]" value="{{ $item->id }}"
{{ in_array($item->id, $some->pluck('id')->toArray()) ? 'checked' : '' }} >

Resources