nero's avatar
Level 1

How to update drop-down country and city in laravel 5?

Yes. how to update drop-down country and city in laravel 5 example strcuture table: table country: id country_name

table city: id country_id city_name

0 likes
5 replies
zachleigh's avatar

Not quite sure what you're asking, but if you want to update location information using drop-down selects, youre going to need to keep all the info on the server and then do ajax requests when the selects change.

nero's avatar
Level 1

Yes. how to update drop-down country and city in laravel 5 example strcuture table: table country: id country_name

table city: id country_id city_name

zachleigh's avatar

So you would need to do something like this:

  • Set an onchange event on the dropdown country menu. This event should fire a javascript function and pass the dropdown element to it.
  • In the javascript function, receive the dropdown element and get the selected value. The value would be the country name. Fire an ajax request to a route on your server and pass the country name.
  • In your php, accept the country name and do a database query to get the cities for the country.
  • Return the city list to the client and use it to populate the city dropdown.
nero's avatar
Level 1

Mr.zachleigh, Could provide an example script ? I still do not understand . please help me.

Please help in the form edit categories and subcategories. How do I change the data sub categories ?. How do I display data sub category by category?.

In the following cases: Data does not appear based on categories. Data sub-category has appeared, but when changing the category of sub-categories do not follow category.

Table:

Tabel Category:

id category_name

Tabel Sub Category

id category_id subcategory_name

Tabel News:

id subcategory_id subject description

View Edit:

{!! Form::model($news,array('url'=>'news/update','files'=>'true'))!!} {!! Form::select('category', $categories, null, ['id' => 'category', 'class' => 'form-control category')]) !!} <option value=""

JS:

var $ = jQuery.noConflict(); $('#category').change(function() { $.get('categories/' + this.value + '/subcategories.json', function(subcategories) { var $subcategory = $('#subcategory'); $subcategory.find('option').remove().end(); $.each(subcategories, function(index, subcategory) { $subcategory.append('' + subcategory.subcategory_name + ''); }); }); }); $(document).ready(function() { $(".category option[value='0']").attr("disabled","disabled"); $(".subcategory option[value='0']").attr("disabled","disabled"); });

Routes:

Route::get('news/categories/{code}/subcategories.json', function($code) { return \App\Subcategory::where('category_id', $code)->get(); }); Route::get('categories/{code}/subcategories.json', function($code) { return \App\Subcategory::where('category_id', $code)->get(); }); Controllers: ================ $data['news']=News::find($id); $categories =Category::lists('category_name', 'id')->all(); $subcategories =Subcategory::lists('subcategory_name', 'id')->all(); return view('news.edit',$data)->with(compact('categories'))->with(compact('subcategories'));

zachleigh's avatar

@nero What exactly dont you understand? Are you using a javascript framework or jquery? I want to help you, but I'm not going to write your application for you.

For your other question, please make a new thread in the forums and be sure to wrap your code in three backticks (`) so that it is formatted.

```

Your code

```

Please or to participate in this conversation.