Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Dynamic select with Javascript Laravel 8

I'm working on a project with Laravel 8, where I need to make a dynamic dropdown in a form to create categories. The first dropdown must show stores and the second one should show the categories of that store. I don't know if I should do this with javascript or some other way.

Form:

<label for="store_id">Store</label>
<select id="store_id" name="store_id" class="custom-select">                 
@foreach($stores as $store)
   <option value="{{ $store->id}}">{{ $store->name}}</option>
@endforeach
</select>

<label for="parent_category">Parent Category</label>
<select  id="parent_category" name="parent_category" class="custom-select">                                              
</select>

Categories table

id store_id parent_category_id name

Stores table

id name

CategoryController.php

public function create()
{              
  $stores = Store::all();
  $categories = Category::all();                    

  $data= [            
    'stores ' => $stores ,
    'categories ' => $categories 
  ];

return view('category.create')->with($data);
}
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can achieve this approach with jQuery:

get store categories through relationship, depending on your relation type One to Many, Many to Many, etc:

public function Storecategories()
{
    return $this->hasMany('App/Models/Categories','store_id');
}

In your controller function and get store categories:

//return store categories with ajax response using relationship
$store_categories = StoreModel::find($store_id)->Storecategories()
//if any where clause you want to add
->where('Your conditions')
->get();

return response()->json(['store_categories'=>$store_categories->toArray() ]);

Send ajax request on selecting store, get categories array and create categories html dynamically:

//Perform action on selection of store
$(document).on('change','#store_id',function(){
   $.ajax({
            type: "POST",
            url: Your ajax url,
            data[store_id:stroe_id , _token:token],
            success: function (data) {
                let categories = data.store_categories;
                let html = '';
                $.each(categories ,function(i,v){
                    html += '<option value="'+v.id+'">'+v.name+'</option>';
                });
                $("#parent_category").html(html);
            }
        });
}); 
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!