How to get the country value based on the country code? - php

This code "$countries = Facades\Countries::all();" returns the country code and code value like:
"DE" => "Germany"
I want to store the user country in the db when the user select the country in a select menu
<div class="form-group">
<label for="country" class="text-gray">Country</label>
<select class="form-control" name="country" id="country">
#foreach($countries as $key => $country)
<option value="{{$key}}">{{$country}}</option>
#endforeach
</select>
</div>
But with:
$user->country = $request->country;
$user-save();
In the db is stored "DE" but I want to store the country value like "Germany". Do you know how to based on the country code get the value so is possible to store the value and not the code?

In the db is stored "DE" but I want to store the country value like "Germany".
You just need to use as:
$countries = Facades\Countries::all();
$user->country = $countries[$request->country];
Just make sure that Facades\Countries::all() does not have performance impact, i guess it pulls out data from db.
OR
Other solution is to remove value attribute from option tag.
<option>{{$country}}</option>

try this, replace {{$key}} with {{$country}} in option
<div class="form-group">
<label for="country" class="text-gray">Country</label>
<select class="form-control" name="country" id="country">
#foreach($countries as $key => $country)
<option value="{{$country}}">{{$country}}</option>
#endforeach
</select>
</div>

Place country name in select options value as well. Replace {{$key}} with {{$country}}:
#foreach($countries as $country)
<option value="{{$country}}">{{$country}}</option>
#endforeach

Related

Why is only the last element of an array being output in Laravel?

I have a select where a value needs to be selected. Select is formed using a foreach loop. I get the values ​​correct and correct, but when I try to put this value into the session, only the last value of the cycle gets into it all the time. Why is that?
<div class="form-group">
<label for="category">Choose category</label>
<select class="form-control" id="category" name="category_slug" onchange = "getSelectValue();">
#foreach($categories as $category)
<option value="{{$category->slug}}">{{$category->name}}</option>
{{session(['key' => $category->slug])}}
#endforeach
</select>
#dump(session('key'))
</div>
In your loop you overwrite the key with each iteration so that only the last category is put into the session.
If you want to put all the categories into the session you should give each item an individual session key like this:
<select class="form-control" id="category" name="category_slug" onchange = "getSelectValue();">
#foreach($categories as $index => $category)
<option value="{{$category->slug}}">{{$category->name}}</option>
{{session(['category_'.$index => $category->slug])}}
#endforeach
</select>

Getting 'old' value of a select input laravel

I'm working on validating my registration form.
If the input is a regular input, i can populate the previous value if it fails like so
<input type="tel" name="phone" value="{{old('phone')}}">
How do I populate the value on a select option?
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
I suggest putting all you ages in an array and pass that array to view:
$ages = ['18-25','25-50','50-75','75+']; // More dynamic and you can extend in anytime
Now changes in html :
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
// 1. first change which is creating your problem
<option {{ old('age_range') ? "" : "selected" }} disabled>Select your age range</option>
// 2. This is just optimization for short code
#foreach($ages as $age)
<option {{old('age_range') ==$age" ? $selected : ""}} value="{{$age}}">{{$age}}</option>
#endforeach
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
So in point 1 you are always applying the selected with first option.
Suppose your old value was 46-55
your html looks like :
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option selected value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
if you take a look at above html there are 2 selected options. Html always picks the first one this is what creating the problem.
Point 1. will check if there is a old value available it will not apply the selected to the first placeholder option.
{!! Form::select('name', $varableyouwanttodisplay, old('name'),
['id'=>'id', 'class' => 'form-control select2', 'data-
placeholder' => 'select','required', 'style' => 'width:100%']) !!}
try like above code.
#php $selected = old('experience') ?: 66; #endphp
and than
{{ Form::select('experience', $experience, $selected, ['class' => 'form-control']) }}
This works in a case when a blade template loads for the first time and doesn't have 'old' value and you need predefined selected option(I.e. 66) for that case.

Set default option from DB in select box

I have a page where user can edit their profile settings. Data are read from db and displayed as default values for many fields like email, city, salutation, first name, last name etc...
But for country field I am getting the list from DB and always the first option is showed (in this case Afghanistan). How can I set this field to default value if user "preselected" the country before? For example if user selected and saved Germany before, I want this option to be shown as default, not Afghanistan.
{!!Form::open(array('url' => 'editUserProfile/'.$user->id, 'files' => true))!!}
<input class="form-control" type="text" name="firstName" value="{{$user->firstName}}" required>
<select class="form-control" name="country">
#foreach($countries as $country)
<option>{{$country->country_name}}</option>
#endforeach
</select>
{!! Form::close() !!}
How can I set $user->countryas default like for a first name?
Here's some code:
<select class="form-control" name="country">
#foreach($countries as $country)
<option value="{{$country->country_name}}" {{ $country->country_name == $user->country? ' selected' : '' }}>{{$country->country_name}}</option>
#endforeach
</select>
I added value attribute so as country name can be passed to server.
And added ternary operator for checking if current $country->country_name equals to $user->country.
This might help:
#foreach($countries as $country)
<option value="{{ $country->id }}" <?php if($user->country_id == $country->id){ echo "selected"; } ?> >{{$country->country_name}}</option>
#endforeach

Populate multi select with selected values in laravel 5.2

I have stored multiselect values in database as string
Now while trying to edit the values in need to fetch this highlighted string as array and send it to edit view , so that i can populate multi select with this selected values.
My code in view for multi select
<div class="form-group">
<label class="control-label col-md-3">News For
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
{!! Form::select('news_todisplay[]',['users'=>'Users','staff'=>'Staff','cinemahall'=>'Cinemahall'],$todisplayarray,array('class'=>'form-control','multiple'=>'multiple')) !!}
</select>
</div>
</div>
Where $todisplayarray is the array fetched from database i.e they are users,staff, cinemahall
My controller code
public function edit($id)
{
$newsdetails=General_news::findOrFail($id);
$todisplayarray=explode(',',$newsdetails->news_todisplay);
return view('admin.editnews',compact('newsdetails','todisplayarray'));
}
Page view-source shows this
<select class="form-control" multiple="multiple" name="news_todisplay[]"><option value="users">Users</option><option value="staff">Staff</option><option value="cinemahall" selected="selected">Cinemahall</option></select>
This only shows last value as selected, where as it should display 3 of the values as selected.
Kindly let me know where I am going wrong.
Find the below answer
<?php
//get the old values from form
$old = old('category');
//get data from database table field
$cate= explode(',',$data->category_ids);
//stay the values after form submission
if($old){ $cate=$old; }
?>
<select id="categories" name="category[]" multiple>
#foreach ($categories as $val)
<option value="{{ $val->id }}" <?php echo in_array($val->id,$cate)?"selected" :"" ;?> >{{ ucfirst($val->category_name) }}</option>
#endforeach
</select>
I've used loop the categories data with id's and match the data stored in the table

Repopulate values in select field

I want to repopulate the old value after validation.I have tried many options and I am not getting.
Here is my code
select name="Skills[]" class="form-control select2" multiple="multiple" id ="Skills"
#foreach($skills as $skill)
option value="{{$skill->skill_id}}" >{{$skill->skill_name}}</option>
#endforeach
/select
correct me if I'm wrong but what you mean by repopulate the old value is to show as selected the option chosen by the user after validation right?
if so then you can try after redirecting to a View the following:
<?php $i=1; ?>
<select name="Skills[]" class="form-control select2" multiple="multiple" id ="Skills">
#foreach($skills as $skill)
<option value="{{$skill->skill_id}}" #if($skill->skill_id==$i++){'selected="selected"'}#endif>{{$skill->skill_name}}</option>
#endforeach
</select>

Categories