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

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' : '' }} >

Related

Laravel old values plus newly checked values in edit template

In my edit.blade, I would like to show categories that were checked before by user + user can check new categories. So if a validation error occurred, the newly checked categories do not become unchecked.
This is what I have now:
#foreach($categories as $category)
<input type="checkbox" id="category_id" name="category_id[]" value="{{ $category->id }}"
{{ $category->posts->contains($post->id) ? 'checked' : '' }}
#if (old('category')==$category->id) ? 'checked' : '' #endif>
<label for="{{$category->id}}"> {{$category->category_name}}</label>
#endforeach
How can I do it?
put the previously selected items in an array in controller and pass it to the view. like
$previouslySelected = [1, 3, 5];
and then you use the old helper to show the selected options.
<input type="checkbox" name="category_id[]" value="{{ $category->id }}"
class="form-check-input" id="category{{ $category->id }}"
#if (in_array($category->id, old('category_id', $previouslySelected ))) checked #endif>
this will first check the values in $previouslySelected array. and when the validation fails, will check the values from old values.

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

Checkbox value in controller in laravel

I am trying to get all the payments that is been selected through the checkboxes and then update the value in the database by '1' which means its paid. But when I try to get the values of checkbox array I only get 1 data in the array and not all selected. Any help?
View
#foreach($sellerID as $p)
<form action="/paid/{{$p->id}}" method="post" enctype="multipart/form-data">
#csrf
<td><input type="checkbox" name="checked[]" value="{{ $p->id }}"></td>
<td> {{$p->order_number}} </td>
<td>
<input id="commission" type="text" class="form-control" name="commission"
value="{{ $p->commission_value }}" readonly autocomplete="notes" style="width: 15%;" autofocus>
</td>
<td> {{$p->product_name}} </td>
<td>
#if($p->sellet_commission_paid == '0')
<button type="submit" class="btn btn-default" style="background-color:red">
<b><em>UNPAID</b></em>
</button>
#else
<b><em>PAID</b></em>
#endif
<td>
</form>
#endforeach
Controller
$checked = $request->input('checked');
// $checkboxes = $request->checked;
dd($checked);
die();
// dd(checkboxes);
you can put almost anything you like inside a form, but not inside a table. table has a more restricted structure which you must follow.
instead of wrapping table rows with forms, you can wrap the whole table with 1 form and submit all of it and just extract the details you need,
You need to put all input element inside form :
<form action="/route">
<input ...../>
<div>
<input ...../>
</div>
</form>
A form-associated element is, by default, associated with its ancestor form element, but may have a form attribute specified to override this.
If a form-associated element has a form attribute specified, then that attribute's value must be the ID of a form element in the element's owner Document.
Try using a foreach loop.
foreach($request->input('checked') as $check){
[sample variable] = $check;
}
The array will now be stored as $check and you should be able to get each value from the input

Loop through collection - target every item above a certain number

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

Laravel wizard system getting data from db

I am creating wizard systeem i create taks and wizard. but i want to show the take as stap not all the taks in one stap.
<div class="slide" id="slide-2" style="display: none">
#foreach($tasks as $task)
<div class="form-group">
<label for="task[{{ $loop->index }}]">{{ $task->name }</label>
<input type="checkbox" name="task[]" id="task[{{ $loop->index }}]" value="{{ $task->id }}">
</div>
#endforeach
</div>
But now I get all task in one time, how can I get one by one?
$tasks is an array and you can get array value one by one by key or index
for example:
$tasks[0]->name display first index of $tasks name or $tasks[0]->id
and $tasks[1]->name to get second and ...
of course there are many ways to get values , it depends on how do you want to use it
for loop or switch or other loops with conditions

Resources