Can't make multiple select in registration form - laravel

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;

Related

Livewire - Dependant Dropdown select

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.

Laravel Livewire Dependent Dropdown does not fill values on edit

I am trying to implement a dependent dropdown using Laravel Livewire along with Jetstream. Everything worked fine, but there is one minor problem though. When I click on edit button, I am trying to fill the given details from the database. But unfortunately, the dropdown details do not fill. However, the text input fetches the value and displays it.
Here is the blade file:
<form>
<div class="form-group">
<x-jet-label for="countryName">Select Country</x-jet-label>
<select class="form-control" name="country_name" id="countryName" wire:model="countryId">
<option value="">-- Select a country --</option>
#foreach($countries as $country)
<option value="{{ $country->country_id}}">{{ $country->country_name }}</option>
#endforeach
</select>
#error('country_id') <span class="text-danger">{{ $message }}</span>#enderror
<x-jet-label for="stateName">Select State</x-jet-label>
<select class="form-control" name="state_name" id="stateName" wire:model="stateId">
<option value="">-- Select a state --</option>
#if (!is_null($states))
#foreach ($states as $state)
<option value="{{ $state->state_id}}">{{ $state->state_name }}</option>
#endforeach
#endif
</select>
#error('state_id') <span class="text-danger">{{ $message }}</span>#enderror
<x-jet-label for="cityName" style="">City Name:</x-jet-label>
<x-jet-input type="text" name="city_name" id="cityName" placeholder="Enter City Name" wire:model="city_name" />
#error('city_name') <span class="text-danger">{{ $message }}</span>#enderror
</div><br>
<x-jet-button wire:click.prevent="update()" class="btn btn-success">Update</x-jet-button>
<x-jet-danger-button wire:click.prevent="cancel()" class="btn btn-danger">Cancel</x-jet-danger-button>
</form>
And edit function is like:
public function edit($city_id)
{
$city = City::findOrFail($city_id);
$this->city_id=$city_id;
$this->city_name=$city->city_name;
$this->countryId=$city->country_id;
$this->stateId=$city->state_id;
$this->updateMode = true;
}
When I select any option manually from the dropdown, it works.
I am new to laravel and am stuck here... Any help would be highly appreciated. Thank you.

How to select only lessons which belong to a particular course in a create form using Laravel and Blade

I have Course and Lesson models and I want to first select a Course and then only the lessons which belong to that course to be available for selection on a dropdown list.
I have created the pivot tabla course_lesson and the relashionship works ok.
<div class="form-group">
<select name="course_id" class="browser-default custom-select">
<option selected>Choose course</option>
#foreach($courses as $course)
<option value="{{$course->id}}">{{$course->title}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="lesson_id" class="browser-default custom-select">
<option selected>Choose lesson</option>
#foreach($courses as $course)
#foreach($course->lesson as $lesson)
<option value="{{$lesson->id}}">{{$lesson->title}}</option>
#endforeach
#endforeach
</select>
</div>

show data of nested foreach loop in two different Select Tag in laravel

I have two Model(Category,Brand) are relate with one to many relation(hasMany) in laravel, Like category hasMany brands. now i want to display brand data in one select tag and category model data in another select tag.
Here is my Controller code
$category=Category::with('brands')->get();
return view('product.add')->with('category',$category);
Here is my view blade.
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($brands->brands as $brand)
<option value="{{$brand->id}}">{{$brand->name}}</option>
#endforeach
</select>
</div>
its not working
You can do something simple like this:
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $cat)
<option value="{{$cat>id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($category as $cat)
#foreach($cat->brands as $brand)
<option value="{{$brand->id}}" category="{{$cat->id}}">{{$brand->name}}</option>
#endforeach
#endforeach
</select>
</div>
and you need to show only the brands for the selected option using javascript(jquery)
$('[name="category_id"]').on('change',function() {
if($(this).val() != '') {
$('[name="brand_id"] option').hide();
$('[name="brand_id"] option[category="'+$(this).val()+'"]').show();
} else {
$('[name="brand_id"] option').show();
}
});

how to validate select box in laravel (without using JS)

<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);

Resources