How to populate old select option values? - php

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

Related

Laravel 6: Dynamic selectbox/droplist population from the database

As stated in the title, I want to dynamically change the available options of an HTML select box, based on the selection of another HTML select box, using PHP only if possible.
So far, I have managed to pass the data from each table into the view but I do not know how to use the primary and secondary keys in this context and without writing logic into the view file, respecting safety precautions.
What I have:
Controller:
...
use Illuminate\Support\Facades\DB;
...
public function create()
{
$sport_categories = DB::table('sport_categories')->get();
$sports = DB::table('sports')->get();
return view('events.create',
[
'sport_categories' => $sport_categories,
'sports' => $sports
]
);
}
View:
<div id="event-sport-category">
<span>Sport Category:</span>
<select name="sport-category">
<option value="default" selected="selected" disabled hidden>Select Sport Category</option>
#foreach($sport_categories as $sport_category)
<option value="{{ $sport_category->id }}" title="{{ $sport_category->desc }}">{{ $sport_category->name }}</option>
#endforeach
</select>
</div>
and
<div id="event-sport">
<span>Sport:</span>
<select name="sport">
<option value="default" selected="selected" disabled hidden>Select Sport</option>
#foreach($sports as $sport)
<option value="{{ $sport->id }}" title="{{ $sport->desc }}">{{ $sport->name }}</option>
#endforeach
</select>
</div>
The database tables are:
sport_categories (id, name, desc)
and
sports (id, sport_category_id, name, desc)
You'll need to use some sort of Javascript for this to work like you want. As #Clint suggested, since PHP is server-side, you won't be able to change your UI at runtime.
The good news is, you can keep everything you've already done. Depending on your comfort level with Javascript, it could be as easy as doing a show/hide of different html inputs based on what is selected.
Using jQuery, you could do something like this:
<div id="event-sport-category">
<span>Sport Category:</span>
<select name="sport-category" id="sportCategory">
<option value="default" selected="selected" disabled hidden>Select Sport Category</option>
#foreach($sport_categories as $sport_category)
<option value="{{ $sport_category->id }}" title="{{ $sport_category->desc }}">{{ $sport_category->name }}</option>
#endforeach
</select>
</div>
<div id="event-sport-one">
<span>Sport:</span>
<select name="sport" id="sport">
<option value="default" selected="selected" disabled hidden>Select Sport</option>
#foreach($sports as $sport)
<option value="{{ $sport->id }}" title="{{ $sport->desc }}">{{ $sport->name }}</option>
#endforeach
</select>
</div>
<div id="event-sport-two">
<span>Sport:</span>
<select name="sport" id="sportTwo">
<option value="default" selected="selected" disabled hidden>Select Sport</option>
#foreach($sportsTwo as $sportTwo)
<option value="{{ $sportTwo->id }}" title="{{ $sportTwo->desc }}">{{ $sportTwo->name }}</option>
#endforeach
</select>
</div>
jQuery
$(document).ready(function(){
$("#sportCategory").change(function(){
var category = $(this).children("option:selected").val();
console.log('selected category: ', category);
if (category == 'foo') {
// show sports related to foo
// hide & reset everything else
...
$('#event-sport-two').show();
$('#event-sport-one').hide();
}
});
});
CSS
#event-sport-two {
display: none;
}
Doing something like that would render/hide all of your available options when the page loads. You'd use CSS to hide the actual form elements, then using jQuery you could show/hide things as needed. This could get messy if you have a lot of things to manage.
Another option would be to make your form a Vue component. You could then use v-show based on what was chosen.
A final alternative (using either way) would be to listen for the input to change -- then make an ajax request to get your data and show the input based on the server's response.
Vue would give you a bit cleaner code. You could get the data and only show what's needed and not have to worry about all the show/hide business. :)
Hope that helps!

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?

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.

I want to display the entire list of countries in the Select Options menu in laravel

I want to display the entire list of countries in the Select Options menu in laravel.
this my data :
this my data
this the controller
public function getCart()
{
$country = Roketin::country()->list()->get();
// dd($country);
$province = Roketin::province()->list('country_code')->get();
// dd($province);
$carts = Cart::getContent();
return view('pay-viewcart', compact('carts', 'country', 'province'));
}
this my blade.php
*COUNTRY
<select class="selectpicker" id="country" name="country">
<option> -- Select One -- </option>
#foreach($country->data[0]->name as $countries)
<option> {{$countries}} </option>
#endforeach
</select>
and error :
Invalid argument supplied for foreach() (View: D:\PPI\projek-1\savanacamp\resources\views\pay-viewcart.blade.php)"
<select class="selectpicker" id="country" name="country">
<option> -- Select One -- </option>
#foreach($country->data as $item)
<option> {{$item->name}} </option>
#endforeach
</select>
or
<select class="selectpicker" id="country" name="country">
<option> -- Select One -- </option>
#foreach($country->data as $item)
<option> {{$item['name']}} </option>
#endforeach
</select>
You are indeed supplying wrong values in the foreach
Update your foreach as follows.
<select class="selectpicker" id="country" name="country">
<option> -- Select One -- </option>
#foreach($country as $c)
<!-- ^--- supply the list here. -->
<option> {{ $c->data[0]->name }} </option>
<!-- ^--- access the name here -->
#endforeach
</select>

Categories