Hi so I'm trying to change my forms for creating an appointment, I currently have a foreach loop for radio buttons and it works but ofcourse it doesn't look great. So i'm trying to instead use a select menu so it look better, but when I try submit my new form it tells me the doctor field must be selected, so it's not getting the actual value of what's selected.
addappointmentform.blade (OLD VERSION as stated above this works but looks bad)
<fieldset>
<legend>Select the Doctor</legend>
#foreach($doctors as $doctor)
<div>
<label for="dirBtn{{$doctor->id}}">
<input id="dirBtn{{$doctor->id}}" type="radio" name="doctor" value="{{$doctor->id}}">Dr
{{$doctor->surname}}
</label>
</div>
#endforeach
</fieldset>
addappointmentform.blade(NEW VERSION, this is what i'm trying to get working)
<fieldset>
<select name="doctorsSelect">
<option selected disabled>Please select a Doctor</option>
#foreach($doctors as $doctor)
<option value="{{ $doctor->id }}"
name="doctor" input id="dirBtn{{$doctor->id}}" value="{{$doctor->id}}">
Dr {{$doctor->surname}}</option>
#endforeach
</select>
</fieldset>
AppointmentController
function addAppointmentDatabase(Request $request)
{
$this->validate($request, [
'time' => 'required|',
'date' => 'required|min:10|max:10|filled',
'doctor' => 'required',
'user' => 'required',
]);
//create a appointment
$appointment = new Appointment();
$appointment->time=$request->time;
$appointment->date=$request->date;
//assign doctor to appointment
$doctor = Doctor::find($request->doctor);
$appointment->doctor()->associate($doctor);
$user = User::find($request->user);
$appointment->user()->associate($user);
//save the appointment
$appointment->save();
return redirect('all');
}
You don't need an id for the option, Also your select needs to have the name of the previous input.
Something like this:
<fieldset>
<select name="doctor">
<option selected disabled>Please select a Doctor</option>
#foreach($doctors as $doctor)
<option value="{{$doctor->id}}">Dr {{$doctor->surname}}</option>
#endforeach
</select>
</fieldset>
Hope this helps.
Your select name is doctorsSelect, change it to doctor:
<select name="doctor">
Related
I want to update two values in my data I want to update the first value as an int and the other one is string.
TABLE STRUCTURE
here is my input, the value aircraft_id is the first one to update and the other one is the aircraft_refistration_number
<select name="aircraft_id" class="form-control" id="">
<option value="0" disabled="true" selected="true"> Select </option>
#foreach ($aircrafts as $aircraft)
<option value="{{ $aircraft->aircraft_id }}">{{ $aircraft->aircraft_registration_number }}</option>
#endforeach
</select>
here is my table
here is where i update the registered_company_name which has to be string but then the output is the aircraft_id
$txtDescript = $request->input('aircraft_id');
$aircraft = DB::table('settings')
->where('id', 4)
->update(['description' => $txtDescript]);
here is the aircraft_id which should be int or id's
$airid = $request->input('aircraft_id');
$aircraft = DB::table('series')
->update(['aircraft_id' => $airid]);
this one perfectly works
here is my output of the problem
WHEREAS it should be like THIS output which should be correct
you can take this longer form,
<select name="aircraft_id" class="form-control" id="">
<option value="0" disabled="true" selected="true" id="airselect"> Select </option>
#foreach ($aircrafts as $aircraft)
<option value="{{ $aircraft->aircraft_id }}">{{ $aircraft->aircraft_registration_number }}</option>
#endforeach
</select>
<input type="hidden" name="aircraft_name" id="aircraft_name" value="">
and set the value of the hidden field using jquery like so
$(document).ready(function(){
$(document).on('change','.airselect',function(){
$selected=$( "#airselect option:selected" ).text();
$('#aircraft_name').val($selected);
});
});
and then adjust your controller like so
$txtDescript = $request->input('aircraft_name');
$aircraft = DB::table('settings')
->where('id', 4)
->update(['
description' => $txtDescript]);
and
$airid = $request->input('aircraft_id');
$aircraft = DB::table('series')
->update(['aircraft_id' => $airid]);
try it and let me here from you, i did not test the code
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
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
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>
I have a form where I put de id's of my bands table inside a select option. When I select an id in the dropdownlist and try to submit the form, the value always remains NULL. How can I retrieve the value I selected?
create.blade.php file :
<form>
<select name="band_id">
#foreach ($bands as $band)
<option value="{{ $band->band_id }}">{{ $band->band_id }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
</form>
My Bandcontroller inside my store action :
$bands->band_id = $input['band_id'];
First of all you are using #foreach ($band as $band), make sure it's not a typo and if not then fix it (Could be $bands as $band and assumed you know what I mean). Anyways, you may also try this:
{{ Form::select('band_id', $bands, Input::old('band_id')) }}
Just pass the $bands variable to the View and Laravel will create the SELECT for you, you don't need to loop manually. To retrieve the value, you may try this:
$band_id = Input::get('band_id');
Laravel 6 or above
//blade
{!!Form::open(['action'=>'CategoryController#storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
#foreach ($cats as $cat)
<option value={{$cat->id}}>{{ $cat->title }}</option>
#endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
// controller
public function storecat(Request $request){
Log::channel('stack')->info('name'.$request->app);
if($request->input('cat')==null){ // retriving option value by the name attribute of select tag
return redirect(route('cats.cat'))->with('error', 'Select an app');
}
$this->validate($request,[
'name'=>'required',
]);
}