samedi 28 octobre 2017

Dynamic dropdown with Ajax from one table in Laravel

I am trying to make a drop down list that update the URL when choosing a city like that :

http://Localhost:8000/Victoria/Melbourne

But the URL always update with empty arrays like that :

http://localhost:8000/[ ]/[ ]

How can i fix that please ?

My table structure like below :

database strutcture

My blade html and Ajax code :

<select id="country_name_id" class="countryName form-control">

                    @foreach ($countries as $country) 

                        <option></option>

                    @endforeach

                </select>

                <br>

                <select class="regionName form-control">

                    <option value="0" disabled="true" selected="true">Choose Region</option>

                </select>

                <br>

                <select id="city_select" class="cityName form-control">
                    <option value="0" disabled="true"selected="true">Choose City</option>

                </select>

My routes :

Route::get('/findRegionName', 'HomeController@findRegion');

Route::get('/findCityName', 'HomeController@findCity');

Route::get('/{regions}/{cities}', 'HomeController@store')->name('user.location.store');

My Home controller :

class HomeController extends Controller
{

public function index(Request $request)

{
    $countries = Location::select('country_name')->groupBy('country_name')->get();

    $regions = self::findRegion($request);

    $cities = self::findCity($request);

    return view('home', compact('countries','regions','cities'));

}

public function findRegion(Request $request)

{
    $data=Location::select('region_name')->groupBy('region_name')->where('country_name',$request->country_name)->distinct()->get();

    return $data;
}

public function findCity(Request $request)

{        
       $data=Location::select('city_name')->groupBy('city_name')->where('region_name',$request->region_name)->distinct()->get();

    return $data;
}

public function store()

{
}

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire