Show phone code of a country selected in dropdown Laravel - laravel

I'm trying to show the phone code based on the country selected. Like when you try to register and while choosing a country the phone code changes at the same time.
here's my code.
<select class="form-control" name="country_id" required>
<option value selected disabled>Select Country</option>
#foreach ($countries as $country)
<option value="{{ $country->id }}" id="shop-country">{{ $country->name }}</option>
#endforeach
</select>
<div class="form-group">
<label>Phone Number</label>
#foreach ($countries as $country)
<span id="phonecode">{{ $country->phonecode }}</span>
#endforeach
</div>

First, you need to populate the select options with a custom attribute, for example, phonecode
<select id="countryList" class="form-control" name="country_id" required>
#foreach ($countries as $country)
<option phonecode="{{ $country->phonecode }}"
value="{{ $country->id }}"
id="shop-country">{{ $country->name }}
</option>
#endforeach
</select>
Remove foreach loop from pone code text.
<div class="form-group">
<label>Phone Number</label>
<span id="phonecode"></span>
</div>
Now, use Javascript to listen to change events on the select list.
<script>
let countryList = document.getElementById("countryList") //select list with id countryList
let phoneCode = document.getElementById('phonecode') //span with id phonecode
countryList.addEventListener('change', function(){
phoneCode.textContent = this.options[this.selectedIndex].getAttribute("phonecode");
});
</script>

Related

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 filter subcategory data from parent category in Laravel8

I want to filter subcategory data according to parent category.
I am getting all my subcategory inside all the categories. I tried many old answers from stackoverflow and youtube but not worked.
I want this filter inside my article post form.
create.article.blade.php
<form action="{{ URL::to('post-article-form') }}" method="post" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="exampleInputEmail1">Article Name <b style="color: red">*</b></label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter article title" name="articleTitle" required>
</div>
<div class="form-group">
<label for="exampleInputEmail1"> Select Article category </label>
<select class="form-control" name="category" id="category" required>
<option value=""> Select </option>
#foreach($categories as $category)
<option value="{{ $category->id }}"> {{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="exampleInputEmail1"> Select Sub category </label>
<select class="form-control" name="subCategory" id="subCategory" required>
<option value=""> Select </option>
#foreach($subCategories as $subCategory)
<option value="{{ $subCategory->id }}"> {{ $subCategory->name }}</option>
#endforeach
</select>
</div>
I used this below script for filter but not worked.
<script>
$('#category').on('change',function(e){
console.log(e);
var cat_id = e.target.value;
//ajax
$.get('/ajax-subcat?cat_id='+cat_id, function(data){
//subcategory
$('#subCategory').empty();
$('#subCategory').append($("<option></option>").val("").html("--Select Sub Category--"));
$.each(data,function(index, subcatObj){
$('#subCategory').append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>');
})
})
});
</script>
My web.php route
//sub category
Route::get('add-sub-category', [SubCategoryController::class, 'create']);
Route::post('post-sub-category-form', [SubCategoryController::class, 'store']);
Route::get('subcategories', [SubCategoryController::class, 'index']);
Route::get("/ajax-subcat", function (Request $request){
$cat_id = $request-> Input::get('cat_id');
$subCategories = SubCategory::where('category_id',$cat_id)->get();
return response()->json($subCategories);
});

Adminlte multi select searching display in laravel

I am using Adminlte 3 Multiple (.select2-purple) in laravel 6, and I got a error like this
I don't know why searching... is showing
Here's my code:
VIEW
<div class="col-md">
<div class="form-group">
<label>Technician</label>
<div class="select2-purple">
<select class="select2" multiple="multiple" data-placeholder="Select a Technician" data-
dropdown-css-class="select2-purple" style="width: 100%;">
#foreach($stations as $station)
<option id="{{ $station->id }}">{{ $station->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
<script>
$('.select2').select2()
</script>
Controller
public function totaltestperform(){
$stations = \DB::table('stations')
->select('id','station')
->orderby('id')
->get();
return view("analytic/total_test_perform",compact('stations'));
}
Here's the output of station variable when I use dd($stations);
Wrong: <option **id="{{ $station->id }}"**>{{ $station->name }}</option>
Use: <option value="{{ $station->id }}">{{ $station->name }}</option>

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

Can't make multiple select in registration form

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;

Resources