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

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>

Related

how to run to foreach loops for single select tag laravel?

I want to show previously selected options as selected, I have two data sets.
blocks that are JSON encoded in a column with country codes.
countries list from the country table
I want to compare both and want to show countries selected that are present in users blocked colmn JSON encoded
$user = User::find(auth()->user()->id);
$blocks = json_decode($user->blocked);
#foreach( Countries::orderBy('country_name')->get() as $country )
<option value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
i tried following but it selects on 1 value either 1st or last
#foreach ($sundaysArray as $key => $value)
#foreach( Countries::orderBy('country_name')->get() as $country )
<option #if( $value == $country->country_code ) selected="selected" #endif value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
#endforeach
Thanks to #gert-b
$user = User::find(auth()->user()->id);
$sundaysArray = json_decode($user->blocked);
#foreach( Countries::orderBy('country_name')->get() as $country )
<option #if (in_array($country->country_code,$sundaysArray)) selected="selected" #endif value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
$selected = explode(",", $products->supplier_id);
<select name="supplier_id[]" multiple="multiple">
#foreach($suppliers as $supplier)
<option value="{{ $supplier->id }}" {{ (in_array($supplier->id, $selected)) ? 'selected' : '' }}>{{ $supplier->name}}</option>
#endforeach
</select>

Duplicate select2 item in the foreach loop

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)

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?

foreach in foreach laravel

I pass two array from controller to view and then i want option to select when the value is the same. But i want to show all data from $arr_1 in select. and my result i get duplicate data in my select.
$arr_1=["1","2","3","4"];
$arr_2=["1","2","4"];
#foreach($arr_1 as $val)
#foreach($arr_2 as $value)
#if($val==$value)
<option selected>{{$val}}</option>
#else
<option>{{$val}}</option>
#endif
#endforeach
#endforeach
Any solution for these?
It can be done without two foreach
$arr_1=["1","2","3","4"];
$arr_2=["1","2","4"];
<select multiple>
#foreach($arr_1 as $val)
#if(in_array($val,$arr_2))
<option val="{{$val}}" selected>{{$val}}</option>
#else
<option val="{{$val}}" >{{$val}}</option>
#endif
#endforeach
</select>
Demo

Categories