Duplicate select2 item in the foreach loop - php

Hello everyone, I'm returning the data with foreach from the dishes table in foods.
I bring the selected ids as selected, but this time it is copied until the selected one.
<select class="form-control select2 " name="lunch" multiple="" data-select2-id="lunch{{$item->id}}" aria-hidden="true">
<optgroup label="Öğle Yemeği" >
#foreach ($foods as $food)
#foreach ($item->dinner as $lunch)
#if ($lunch != $food->id)
#continue
#else
<option value="{{$food->id}}" selected >{{$food->name}}</option>
#break
#endif
#endforeach
<option value="{{$food->id}}">{{$food->name}}</option>
#endforeach
</optgroup></select>
My output of code like this: http://prntscr.com/110203m
How can i fix that?

Your code repeats 2 times
<option value="{{$food->id}}" selected >{{$food->name}}</option>
Try using this code
<select class="form-control select2" name="lunch" multiple="" data-select2-id="lunch{{$item->id}}" aria-hidden="true">
<optgroup label="Öğle Yemeği">
#foreach ($foods as $food)
<option value="{{$food->id}}" #if(in_array($food->id, $item->dinner)) selected #endif>{{$food->name}}</option>
#endforeach
</optgroup>
</select>
Still "continue" can be used like this
#continue($lunch != $food->id)

Related

How to get id of selected value from dropdown and use it in other dropdown in laravel?

I'm newbie at Laravel and on stackoverflow. I'm making an app for registration. I want to show all those ids of section where class_id is selected in upper dropdown.
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',$cid)->get();
$counterrr=0;
Dropdown menu for Class
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
Dropdown menu for Section where I want to get all the values of section where class_id is upper selected
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
Please help me in these case. You can freely ask anything if you want to.
You do not need to set a $counter in order to set the first select option to selected. Instead, you can check if the $key == 0:
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $key => $cc)
<option {{ $key == 0 ? 'selected="selected"' : '' }} value="{{$cc->id}}">{{$cc->title}}</option>
#endforeach
</select>
Here we use a ternary expression to echo out the selected option based on if $key equals 0. If not, it will not echo out anything.

How to fetch value from objects in laravel?

I m making an app in which I want to get value of an object and also I want to get id while creating new user.
I was using drop down menu, and want to get id which was selected in previous dropdown.
#php
use Illuminate\Support\Facades\DB;
$uid=DB::table('users')->where('role_id','4')->get();
$counter=0;
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',array('class_id' => $cid->id('class_id')))->get();
$counterrr=0;
#endphp
<select user="" name="parent_id">
<option value="null">User</option>
#foreach($uid as $user)
#if($counter==0)
<option selected="selected" value="{{$user->id}}">{{$user->name}}</option>
{{$counter++}}
#else
<option value="{{$user->id}}">{{$user->name}}</option>
#endif
#endforeach
</select>
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
As now I want that the object shows the value of selected id in class-dropdown menu and I want to put it in selected list of section
$sec=DB::table('sections')->where('class_id',$cid)->get();
I' m having issues in writing syntax over here.('class_id',$cid)
Also I want that a id on which this new user is going to be created , how to get that?

Laravel blade hide select options in foreach loop

Hi I'm trying to show/hide select options in blade view in Laravel.
im passing in the Model ($assessments) to the view:
[{"id":1,"user_id":1,"name":"Appointment 1","created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"},{"id":2,"user_id":1,"name":"Appointment 2","created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"}]
In the view I've got:
<select name="assessment" required>
<option selected>Select...</option>
#foreach($assessments as $assessment)
#if ($assessment->name == 'Appointment 1')
<option value="Appointment 1">Appointment 1</option>
#endif
#if ($assessment->name == 'Appointment 2')
<option value="Appointment 2">Appointment 2</option>
#endif
#if ($assessment->name == 'Appointment 3')
<option value="Appointment 3">Appointment 3</option>
#endif
#endforeach
<option value="Follow Up Phone Call">Follow Up Phone Call</option>
<option value="Home Assessment">Home Assessment</option> -->
</select>
If the if statement equals to True, I don't know if you can put a continue to bypass in the foreach loop? otherwise, it displays values mulitple times from loop.
Let's assume that in the controller, you have got your results and put it in $assesments variable. One thing you can do is that you can define a new array and send it to your view:
$assesmentNames = [];
foreach($assesments as $assesment) {
$assesmentNames[$assesment->name] = $assesment->name;
}
return view('myview', compact('assesments', 'assesmentNames'));
Now automatically the duplicates are gone. Then do this in your view:
#foreach($assessmentNames as $assessmentName)
<option value="{{ $assessmentName }}"> {{ $assessmentName }} </option>
#endforeach
your may use "continue" statement in simple php tags within loop if you want to skip the particular iteration

How to populate old select option values?

I have a select box in my form that looks like below :
<select class="form-control" name="category_id" id="category_id">
#if(count($category_list)>0)
<option value="null">Pick a Category</option>
#foreach($category_list as $cl)
<option value="{{$cl->id}}">{{$cl->name}}</option>
#endforeach
#else
<option>No categories found</option>
#endif
</select>
My question is, how do I populate the select option with old selected option when the validation fails ? By populate I mean like {{old('input')}}..
I tried this way :
#if(old('category_id')
<option value="{{old('category_id')}}">{{old name ???}}</option>
#endif
But it will only get the category_id value, not the category name. How to get the old category name as well ? that's all and thanks!
you can do like this
#if(count($category_list)>0)
<option value="null">Pick a Category</option>
#foreach($category_list as $cl)
<option value="{{$cl->id}}" #if(old('category_id') == $cl->id) selected="selected" #endif>{{$cl->name}}</option>
#endforeach
#else
<option>No categories found</option>
#endif

Filling a <select> with the saved option as chosen in Laravel

I am trying to fill a <select> dynamically with the chosen option as first option.
Here is my attempt:
<select id= "my_id" name="my_name" size="1" class="my_class">
#foreach($items as $item)
#if($selected_item->item_id == $item->id)
<option selected value="{{$item->id}}">{{$item->name}}</option>
#endif
#endforeach
#foreach($items as $item)
#if($selected_item->item_id != $item->id)
<option value="{{$item->id}}">{{$item->name}}</option>
#endif
#endforeach
</select>
$selected_item contains the ID of the item in other table. $item contains the ID and names of all items.
Any ideas?
Assumptions:
i) $selected_item is an object and doest have property item_id.
ii) $items is an array of objects.
then following code should produce desired results.
<select id= "my_id" name="my_name" size="1" class="my_class">
#foreach($items as $item)
#if($selected_item->item_id == $item->id)
<option selected value="{{$item->id}}">{{$item->name}}</option>
#else
<option value="{{$item->id}}">{{$item->name}}</option>
#endif
#endforeach
</select>

Categories