<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Publication Status</label>
<div class="col-sm-10">
<select name="publicationStatus" class="form-control">
<option value="">Select Manufacturer</option>
<option value="1">Published</option>
<option value="0">Unpublished</option>
</select>
<span class="text-danger">{{$errors->has('publicationStatus')? $errors->first('publicationStatus'):''}}</span>
</div>
</div>
my controller
public function storeManufacturer(Request $request){
$this->validate($request,[
'manufacturerName'=>'required',
'manufacturerDescription'=>'required',
'publicationStatus'=>'required',
]);
above 'manufacturerName' & 'manufacturerDescription' is working but publicationStatus is not validate in the same way.Maybe the reason is that contain multiple value.
You could check data of $request with dd() in your controller firstly:
dd($request->publicationStatus);
Related
I need a little help..
In case of validation errors, when the page is reloaded I lose the information entered previously.
This is my code:
Controller:
public function create()
{
$nationalities = array('Italian','Brazilian','Spanish','Romanian');
return view('guest.create', ['nationalities'=>$nationalities);
}
public function store(Request $request)
{
$request->validate([
'nationality' => 'required'
]);
Guest::create([
'nationality' => $request->nationality
]);
return redirect(route('guest.index'));
}
View:
<div class="mb-3">
<label for="nationality" class="form-label fw-bold">Select<span class="required">*</span></label>
<select class="form-select" id="nationality" name="nationality" aria-label="Floating label select example">
#foreach($nationalities as $nationality)
<option value="{{$nationality}}">{{$nationality}}</option>
#endforeach
</select>
</div>
How can I modify the code so that this doesn't happen?
How can you select back what failed if you did not even ask for the old value?
This may or not work depending on how you are validating, but you did not want to show that...
<div class="mb-3">
<label for="nationality" class="form-label fw-bold">Select<span class="required">*</span></label>
<select class="form-select" id="nationality" name="nationality" aria-label="Floating label select example">
#foreach($nationalities as $nationality)
<option value="{{$nationality}}" {{ old('nationality') === $nationality) ? 'selected="selected"' : '' }}>{{$nationality}}</option>
#endforeach
</select>
</div>
<option value="{{$nationality}}" #selected(old('nationality')==$nationality)>
{{$nationality}}
</option>
add the #selected directive in blade.
I have created a dependant dropdown with livewire like this:
class CreateAppointment extends Component
{
public $doctorCategories;
public $doctors;
public $selectedDoctorCategory = NULL;
public function mount(){
$this->doctorCategories = Doctor_Categories::all();
$this->doctors = collect();
}
public function updatedSelectedDoctorCategory($doctor_type_ID)
{
if(!is_null($doctor_type_ID)){
$this->doctors = Doctor_Categories::with('doctors')->find($doctor_type_ID);
}
}
}
And this is in my blade file:
<div class="form-group row">
<label for="doctor_categories" class="col-md-4 col-form-label text-md-right">Doctor Categories</label>
<div class="col-md-6">
<select wire:model="selectedDoctorCategory" class="form-control">
<option value="">Choose category</option>
#foreach($doctorCategories as $cat)
<option value="{{ $cat->id }}">{{ $cat->doctor_category }}</option>
#endforeach
</select>
</div>
</div>
#if (!is_null($selectedDoctorCategory))
<div class="form-group row">
<label for="doctor" class="col-md-4 col-form-label text-md-right">Doctor</label>
<div class="col-md-6">
<select class="form-control" name="doctor_id">
<option value="" selected>Choose Doctor</option>
#foreach($doctors->doctors as $doctor)
<option value="{{ $doctor->id }}">{{ $doctor->name }}</option>
#endforeach
</select>
</div>
</div>
#endif
</div>
It works well but when i select "Choose category" again it throws an error saying attempted to read property doctors on null.How can i avoid this?
The issue here is, that livewire does not apply the TrimStrings and ConvertEmptyStringsToNull middlewares on its requests. This is by design and is not a bug. It can be reintroduced (see the comments to the linked issue) but I advise caution doing so. With select it should work fine, but with input it can mess things up.
Instead of checking for is_null, check for empty and you should be fine.
I have a dropdown on my page to load districts. And I have another dropdown to load Divisions according to that selected district. The below code I have got all the divisions. But I want only the divisions according to the selected districts. How can I do?
#php
$districtAll=\App\District::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="district">9. District</label>
<div class="col-md-8">
<select name="district">
<option type="text" class="detail-wp" value="">Select District</option>
#foreach($districtAll as $all)
<option value="{{$all->DISTRICT_ID}}">{{$all->DISTRICT}} </option>
#endforeach
</select>
</div>
</div>
<br>
#php
$getDsAll=\App\DsDivision::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="ds_division">10. DS Division</label>
<div class="col-md-8">
<select name="ds_division" class="form-control">
<option type="text" class="detail-wp" value=""></option>
#foreach($getDsAll as $all)
<option value="{{$all->DIVISION_ID}}">{{$all->DIVISION}} </option>
#endforeach
</select>
</div>
</div>
You can do it using ajax request:
$('select[name=district]').on('change', function(){
$districtId = $(this).val();
$.get('/division/district/'+$districtId).done(function(res){
$.each(res, function (key, value) {
$('select[name=ds_division]').append(`<option value='` + value.id + `'>` + value.name +`</option>`);
});
});
to make this working you will need a route like
Route::get("/division/district/{district}", "DivisionController#getByDistrictId");
DivisionController#getByDistrictIdwill be
public function getByDistrictId($district)
{
return Division::where('district_id',$district)->get();
}
this code just to give you an idea of how you can do it.
I use the basic registration form but I want to add a multiple select, a User can have multiple Skills and select them when he register.
My problem is that when I select multiples values Laravel only take the last one, so I search on the web for a solution and they say to add [] after my class name, so this is what I do :
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" multiple="" class="form-control">
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
But now, when I submit the form the page reload and stay in the register form, and the user is not register... I don't know what to do, anyone of you have a solution ?
Here is how I check the competences in my registerController
$competences = $data['competences'];
foreach ($competences as $comp){
$user->competences()->save(Competence::find($comp));
}
Use multiple="multiple" OR just multiple in blade.php:
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" class="form-control" multiple>
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
then in your controller, verify that you're actually getting the data in the form of an array by just printing $data
return $data;
I've made a dropdown in laravel but when I save the request it won't save the value that was selected. How do can I achieve this?
This is my code in my view:
<div class="form-group">
<label for="content" class="col-lg-2 control-label">Karakterrit</label>
<div class="col-lg-10">
<select class="form-control input-sm" name="karakterrit_id">
#foreach($karakterrit as $krit)
<option value="{!! $krit->id !!}">{!! $krit->rit !!}</option>
#endforeach
</select>
</div>
</div>
And this is my controller where I save the request:
public function store(Request $request)
{
$rit = new Rittenregistratie($request->all());
Auth::user()->Rittenregistratie()->save($rit);
return redirect('/create')->with('message','Rit '.$rit->id.' is aangemaakt.');
}
It saves everything right except the value from the dropdown which must be a number.