Laravel: Condition in select dropdown - php

I want to get the value of my Select Dropdown list from my view to my controller
Is there any way i can get this? :(
Here's my dropdown view
{{ Form::select('status', ['All', 'Absent', 'Late', 'Others']) }}
Here's where i want to condition my selected value
public function filterSummaryStudent()
{
if(Input::has('status') == 'All')
{
(other codes here)
}
I've been getting a blank page when i call this.. Please help. Thank you!

You must specify the value of select dropdown as associative arrays if you want to check the value as a string. Right now your select dropdown code the value is define using numeric index of the array. When you check the Input::has('status') == 'All', of course laravel will return false.
Your code
{{ Form::select('status', ['All', 'Absent', 'Late', 'Others']) }}
HTML Output
<select name="status">
<option value="0">All</option>
<option value="1">Absent</option>
<option value="2">Late</option>
<option value="3">Others</option>
</select>
Correct code
{!! Form::select('status', ['All' => 'All', 'Absent' => 'Absent', 'Late' => 'Late', 'Others' => 'Others']) !!}
HTML Output
<select name="status">
<option value="all">All</option>
<option value="absent">Absent</option>
<option value="late">Late</option>
<option value="others">Others</option>
</select>
If you write like the above code you can check the select dropdown like this.
if(Input::has('status') == 'All') {
// Your code
}

Related

Laravel: Use same column as option name and its value in select box/drop-down?

I'm trying to create a form with a select box in Laravel.
View
{{ Form::select('cmp_type', $cmp_types, null, $attributes = ['class' => 'form-control']) }}
Controller
public function showAddCompany()
{
$cmp_types = cmpTypes::where('status', true)->pluck('type');
return view('addCompany', compact('cmp_types'));
}
This is generating a select box.
<select class="form-control" id="cmp_type" name="cmp_type">
<option value="0">public</option>
<option value="1">pvt Ltd</option>
<option value="2">LLP</option>
<option value="3">NPO</option>
<option value="4">partnership</option>
<option value="5">proprietorship</option>
<option value="6">one person</option>
</select>
What can I do to get the select box as?:
<select class="form-control" id="cmp_type" name="cmp_type">
<option value="public">public</option>
<option value="pvt Ltd">pvt Ltd</option>
<option value="LLP">LLP</option>
<option value="NPO">NPO</option>
<option value="partnership">partnership</option>
<option value="proprietorship">proprietorship</option>
<option value="one person">one person</option>
</select>
I believe what is happening is that when you call pluck, you are being returned a numeric array (Collection?) of the values and those numeric keys are being used for the value property and the actual value of said key is being used as the display, what if you did:
$cmp_types = cmpTypes::where('status' , true )->pluck('type');
$cmp_types = array_combine($cpm_types, $cmp_types);
Reading Material
array_combine
array_combine() helped me.
the solution is:
public function showAddCompany()
{
$cmp_types_obj = cmpTypes::where('status' , true )->pluck('type');
$cmp_types = json_decode($cmp_types_obj);
$cmp_types = array_combine($cmp_types, $cmp_types);
return view('addCompany', compact('cmp_types'));
}
my query is returning an object, so i converted it into an array first and then i combined it with itself to get an associate array as
['public']=>'public,['private']=>'private',...

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.

In my Yii2 dropdown list I need my 'id' column to be the value of my options and the name column to be what the user sees in the select option

In my Yii2 dorpdown list I need my 'id' column to be the value of my options and the name column to be what the user sees in the select dropdownlist
My code is outputting the below:
<select id="gatewayproviders-id" class="form-control" name="GatewayProviders[id]">
<optgroup label="0">
<option value="id">Authorize.net</option>
</optgroup>
<optgroup label="1">
<option value="id">NMI</option>
</optgroup>
</select>
However I want it to output the below:
<select id="gatewayproviders-id" class="form-control" name="GatewayProviders[id]">
<option value="1">Authorize.net</option>
<option value="2">NMI</option>
</select>
My yii2 code that is generating this is below:
<?php
$gatewayTypes = \app\models\GatewayProviders::find()->select('gateway_provider')->orderBy('gateway_provider')->asArray()->all();
$gatewayProviders = new \app\models\GatewayProviders();
?>
<?= $form->field($gatewayProviders, 'id')->dropDownList(\app\models\GatewayProviders::find()->select(['id' => 'gateway_provider'])->orderBy('id')->asArray()->all())?>
Any help on this would be greatly appreciated!
You should use ArrayHelper::map() to create map of types (names indexed by IDs):
<?php
$gatewayProviders = new \app\models\GatewayProviders();
$gatewayTypes = \app\models\GatewayProviders::find()
->select(['id', 'gateway_provider'])
->orderBy('gateway_provider')
->asArray()
->all();
$types = \yii\helpers\ArrayHelper::map($gatewayTypes, 'id', 'gateway_provider');
?>
<?= $form->field($gatewayProviders, 'id')->dropDownList($types)?>

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

Selection box option issue in laravel

I had this problem while submitting form
blade
{{ Form::label('qualification', '') }}</th><td>{{ Form::select('qualification', $qualification, $jobs_list->qualification,array('class' => 'form-control')) }}
In this $qualification my full database list is there and $jobs_list->qualification is my selected value.but again am changing value and submitting form it stored as number because ex : value ="numbers"
controller:
$field = ToQualification::select('value')->get();
$qualification = array();
foreach($field as $user_qualification){
$qualification[] = $user_qualification->value;
}
generated html by blade code:
<select class="form-control" id="qualification" name="qualification">
<option value="0">Associate's</option>
<option value="1">Bachelor's</option>
<option value="2">Diploma</option>
<option value="3" selected="selected">Doctoral</option>
<option value="4">High Schools</option>
<option value="5">Master's</option>
</select>
anyone tell how to display name and id in table same as value in selection box?

Categories