show data of nested foreach loop in two different Select Tag in laravel - 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();
}
});

Related

How do I search in Laravel using selects?

On my test site I have a search by cities and categories, which are selected using a select
<div class="form-group">
<select class="selectpicker" name="sCity">
#foreach($cities as $city)
<option value="{{$city->slug}}">{{$city->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select class="selectpicker" name="sCat">
#foreach($categories as $cat)
<option value="{{$cat->slug}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
And such a method for searching
public function search(Request $request)
{
$result = DB::table('ads')
->where('city_slug', $request->sCity)
->orWhere('category_slug', $request->sCat)
->get();
dd($result);
}
If you search for one select, then the search is correct, but if you set values ​​to two selections at once, then only the first is triggered, and the second is ignored. How can you fix this?
Try this:
$result1 = DB::table('ads')
->where('city_slug', $request->sCity)
->get();
$result2 = DB::table('ads')
->where('category_slug', $request->sCat)
->get();
$result = $result1->merge($result2):
you could do something like this
in your view
<div class="form-group">
<select class="selectpicker" name="sCity">
<option value=0>please select one</option>
#foreach($cities as $city)
<option value="{{$city->slug}}">{{$city->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select class="selectpicker" name="sCat">
<option value=0>please select one</option>
#foreach($categories as $cat)
<option value="{{$cat->slug}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
and in your controller
$city_slug='';
$category_slug='';
if($request->sCity!=0)
{
$city_slug=' city_slug='.$request->sCity;
}
if($request->sCat!=0)
{
$category_slug=' or category_slug='.$request->sCat;
}
$result = DB::table('ads')
->whereRaw($city_slug.$category_slug)
->get();
dd($result);

Show phone code of a country selected in dropdown 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>

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>

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>

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