<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chandni Soni</title>
    <description>The latest articles on DEV Community by Chandni Soni (@chandni279).</description>
    <link>https://dev.to/chandni279</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F135694%2F71ac2c52-3272-4ddb-b5ad-219b74301fd6.jpg</url>
      <title>DEV Community: Chandni Soni</title>
      <link>https://dev.to/chandni279</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chandni279"/>
    <language>en</language>
    <item>
      <title>Laravel : Country - State - City Dropdown with AJAX</title>
      <dc:creator>Chandni Soni</dc:creator>
      <pubDate>Fri, 08 Mar 2019 08:19:36 +0000</pubDate>
      <link>https://dev.to/chandni279/laravel--country---state---city-dropdown-with-ajax-hnn</link>
      <guid>https://dev.to/chandni279/laravel--country---state---city-dropdown-with-ajax-hnn</guid>
      <description>

&lt;p&gt;Many people have struggle with this topic and they can not get the output and some also complains that they get empty dropdown, so here I am writing my second post on medium and sharing my self tried code snippet with html-javascript file, controller file and route file. So let's begin...&lt;/p&gt;

&lt;p&gt;First we need a blade.php file:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        &amp;lt;select class="form-control" name="country" id="country"&amp;gt;
            &amp;lt;option value=""&amp;gt;Select Country&amp;lt;/option&amp;gt;
            @foreach ($countries as $country) 
                &amp;lt;option value="$country-&amp;gt;country_id"&amp;gt;
                 $country-&amp;gt;country_name
                &amp;lt;/option&amp;gt;
            @endforeach
        &amp;lt;/select&amp;gt;

        &amp;lt;select class="form-control" name="state" id="state"&amp;gt;
        &amp;lt;/select&amp;gt;

        &amp;lt;select class="form-control" name="city" id="city"&amp;gt;
        &amp;lt;/select&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we need a JavaScript file:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        $('#country').change(function(){
        var cid = $(this).val();
        if(cid){
        $.ajax({
           type:"get",
           url:" url('/state') /"+cid, **//Please see the note at the end of the post**
           success:function(res)
           {       
                if(res)
                {
                    $("#state").empty();
                    $("#city").empty();
                    $("#state").append('&amp;lt;option&amp;gt;Select State&amp;lt;/option&amp;gt;');
                    $.each(res,function(key,value){
                        $("#state").append('&amp;lt;option value="'+key+'"&amp;gt;'+value+'&amp;lt;/option&amp;gt;');
                    });
                }
           }

        });
        }
    });
    $('#state').change(function(){
        var sid = $(this).val();
        if(sid){
        $.ajax({
           type:"get",
           url:"url('/city')/"+sid, **//Please see the note at the end of the post**
           success:function(res)
           {       
                if(res)
                {
                    $("#city").empty();
                    $("#city").append('&amp;lt;option&amp;gt;Select City&amp;lt;/option&amp;gt;');
                    $.each(res,function(key,value){
                        $("#city").append('&amp;lt;option value="'+key+'"&amp;gt;'+value+'&amp;lt;/option&amp;gt;');
                    });
                }
           }

        });
        }
    }); 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After that we need to create a controller which fetch all data we need from the database. So here is a code of Controller file:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//For fetching all countries
public function getCounties()
{
    $countries= DB::table("countries")-&amp;gt;get();
    return view('index')-&amp;gt;with('countries',$countries);
}

//For fetching states
public function getStates($id)
{
    $states = DB::table("states")
                -&amp;gt;where("country_id",$id)
                -&amp;gt;pluck("state_name","id");
    return response()-&amp;gt;json($states);
}

//For fetching cities
public function getCities($id)
{
    $cities= DB::table("cities")
                -&amp;gt;where("state_id",$id)
                -&amp;gt;pluck("city_name","id");
    return response()-&amp;gt;json($cities);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;and now last but not the least the route file, which is use to reach to the functions we have created. So let's see the code of rote file:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('/getCountries','YourController@getCountries');
Route::get('/getStates/{id}','YourController@geStates');
Route::get('/getCities/{id}','YourController@geCities');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Please put curly brackets around &lt;strong&gt;url:"(two start curly braces here)url('/state')(two end curly braces here)/"+cid.&lt;/strong&gt; Tried it to put on code but it shows me an error that &lt;em&gt;Liquid error: Liquid variables are disabled&lt;/em&gt;&lt;br&gt;
If anyone has solution for the same then please comment here so next time when I upload code snippets I can use that.&lt;/p&gt;

&lt;p&gt;Happy Codding...&lt;br&gt;
Thank you for reading...&lt;/p&gt;


</description>
      <category>laravelajaxdropdown</category>
      <category>dropdown</category>
      <category>laravel57</category>
    </item>
    <item>
      <title>Hello...!</title>
      <dc:creator>Chandni Soni</dc:creator>
      <pubDate>Tue, 26 Feb 2019 05:16:42 +0000</pubDate>
      <link>https://dev.to/chandni279/hello-k35</link>
      <guid>https://dev.to/chandni279/hello-k35</guid>
      <description>&lt;p&gt;Hello, I am writing here for the first... I don't know what to say. OK So let's start with my introduction, I am a Developer by profession. I really love building websites, APIs and stuff. I have one year of experience of developing. And now if I talk about my hobbies, they are totally  different from my profession. My hobbies are acting, dancing and singing. I have participate many dramas, dancing and singing competition during my school and college time.&lt;br&gt;
Thank you for reading...!&lt;br&gt;
Happy Coding..!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
