Kanchan186's avatar

console.log(data) shows null value please check my code. why it shows null value?

view

 <form class="m-t-40" method="post" action="area" enctype="multipart/form-data">
                                    {{csrf_field()}}


                                  <div class="form-group">
                                        <h5>Select Country <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                             <select class="form-control" name="company_country" id="company_country" onchange="getZone(this.value)" required>
                                                     <option>--Select country--</option>
                                             @foreach($country as $st)
                                                     <option value="{{$st->country_id}}">{{$st->country_name}}</option>
                                             @endforeach
                                        </select>

                                        </div>
                                       
                                    </div>

                                  


                                    <div class="form-group">
                                        <h5>Zone Name<span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                             <select class="form-control" name="zone_id" onchange="getRegion(this.value)" required>
                                                <option>--Select Zone--</option>
                                            
                                        </select>

                                        </div>
                                       
                                    </div>

                                    <div class="form-group">
                                        <h5>Region Name <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                             <select class="form-control " name="region_id" onchange="getState(this.value)" required>
                                                <option>--Select Region--</option>
                                           
                                        </select>


                                         

                                        </div>
                                       
                                    </div>
                                       
                                    
                                    <div class="form-group">
                                        <h5>Select State <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                             <select class="form-control" name="state_id" id="state_id"  required>
                                            <option>--Select State--</option>    
                                                     
                                        </select>

                                        </div>
                                       
                                    </div>


                                <div class="form-group">
                                        <h5>Area Name <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            <input type="text" name="area_name" id ="area_name" class="form-control" required data-validation-required-message="This field is required" required> </div>
                                        </div>




                                    <div class="form-group">
                                        <h5>Select State Area <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            <select class="select2 m-b-10 select2-multiple" name="state_area[]" id="state_area" style="width: 100%" multiple="multiple" data-placeholder="Choose" required data-validation-required-message="Please select state area" required>
                                    
                                    @foreach($city as $ct)
                                    <option value="{{$ct->city_id}}">{{$ct->city_name}}</option>
                                    @endforeach
                                        
                                   
                                </select>
                                           

                                        </div>
                                       
                                    </div>
                                    

                                    
                                            <div class="text-xs-right">
                                                <button type="submit" class="btn btn-info">Submit</button>
                                                <button type="reset" class="btn btn-inverse">Cancel</button>
                                            </div>
                                </form>

script

 function getZone(country)
                            {
                               if(country) {
                                        $.ajax({
                                            url: '{{url('/')}}/zone/ajax/'+country,
                                            type: "GET",
                                            dataType: "json",
                                            success:function(data) {
                                                 console.log(data)

                                                $('select[name="zone_id"]').empty();
                                                $('select[name="zone_id"]').prepend('<option value="">--Select Zone--</option>');
                                                $.each(data, function(key, value) {
                                                    $('select[name="zone_id"]').append('<option value="'+ key +'">'+ value +'</option>');
                                                });

                                            }
                                        });
                                    } else{
                                         $('select[name="zone_id"]').empty();
                                    }
                            }
                                               




 
 function getRegion(zone)
                            {
                               if(zone) {
                                        $.ajax({
                                            url: '{{url('/')}}/region/ajax/'+zone,
                                            type: "GET",
                                            dataType: "json",
                                            success:function(data) {
                                        
                                             console.log(data);
                                                $('select[name="region_id"]').empty();
                                                $('select[name="region_id"]').prepend('<option value="">--Select Region--</option>');
                                                $.each(data, function(key, value) {
                                                    $('select[name="region_id"]').append('<option value="'+ key +'">'+ value +'</option>');
                                                });

                                            }
                                        });
                                    } else{
                                         $('select[name="region_id"]').empty();
                                    }
                            }

 function getState(region)
                            {
                               if(region) {
                                        $.ajax({
                                            url: '{{url('/')}}/state/ajax/'+region,
                                            type: "GET",
                                            dataType: "json",
                                            success:function(data) {
                                        
                                             console.log(data);
                                                $('select[name="state_id"]').empty();
                                                $('select[name="state_id"]').prepend('<option value="">--Select State--</option>');
                                                $.each(data, function(key, value) {
                                                    $('select[name="state_id"]').append('<option value="'+ key +'">'+ value +'</option>');
                                                });

                                            }
                                        });
                                    } else{
                                         $('select[name="state_id"]').empty();
                                    }
                            }

controller

 public function ZoneAjax($country)
    {
        $zone = Zone::where("country_id",$country)
                    ->pluck('zones.zone_name','zones.zone_id')->all();
        return json_encode($zone);
    }

    public function RegionAjax($zone)
    {
        $region = Region::where("zone_id",$zone)
                    ->pluck('regions.region_name','regions.region_id')->all();
        return json_encode($region);
    }

    public function StateAjax($region)
    {
        $state = State::where("region_id",$region)
                    ->pluck('states.state_name','states.state_id')->all();
        return json_encode($state);
    }

web.php

Route::get('zone/ajax/{country}','AreaController@ZoneAjax');

Route::get('region/ajax/{zone}','AreaController@RegionAjax');

Route::get('state/ajax/{region}','AreaController@StateAjax');


this gives null value

http://localhost/partsanalysis/public/state/ajax/16
0 likes
16 replies
jonhCR's avatar

Can you try to clean your route cache?

php artisan route:cache

Otherwise you can review in the cliente side the network bar, and check the request

Nakov's avatar

@kanchan186 and do you have a state for that region? You can check that in your database.

Check the network tab as well to see which state gives that result.

Null is different than an empty array.. for you it seems like there is no state in the database for that region.

add a dump($region); in your controller as a first line, and inspect in the network tab if the correct region_id is being used.

Kanchan186's avatar

@jonhcr i updated my code

i want to fetch values from region_areas table

in controller

 public function StateAjax($region_area)
    {
        $state = State::where("region_id",$region_area)
                    ->pluck('states.state_id','states.state_id')->all();
        return json_encode($state);
    }

in script

function getState(region_area)
                            {
                               if(region_area) {
                                        $.ajax({
                                            url: '{{url('/')}}/state/ajax/'+region_area,
                                            type: "GET",
                                            dataType: "json",
                                            success:function(data) {
                                        
                                             console.log(data);
                                                $('select[name="state_id"]').empty();
                                                $('select[name="state_id"]').prepend('<option value="">--Select State--</option>');
                                                $.each(data, function(key, value) {
                                                    $('select[name="state_id"]').append('<option value="'+ key +'">'+ value +'</option>');
                                                });

                                            }
                                        });
                                    } else{
                                         $('select[name="state_id"]').empty();
                                    }
                            }

in web.php

Route::get('state/ajax/{region_area}','AreaController@StateAjax');

please check my code

Kanchan186's avatar

@nakov first code is wrong i realize my mistake , i am trying to fetch data from region table where state_id is not present.

and i updated my code please check updated code actually i want to fetch data from region_areas table and i made changes for that

still it gets null value

Nakov's avatar

@kanchan186 but [] is not equal with null so you are actually getting an empty collection, which means that your controller code is running, but for the ID that you are checking there is no State. So please check the browser's network tab, and play around with dump() helper to print out which ID are you sending, and which ID is being used in the controller code.

public function StateAjax($region_area)
{
    dump($region_area);
    $state = State::where("rarea_id",$region_area)
                    ->pluck('states.state_id','states.state_id')->all();
    return json_encode($state);
}

And check the network tab for this request.

And you are using State model here, but you've shown code from region_areas table. Do you have protected $table = 'region_areas'; in your State model?

Kanchan186's avatar

@nakov in console

{1: "Zone 1", 3: "zone 2"}
{15: "South India"}
 []

gives output this type state value is null []

in network tab output

Kanchan186's avatar

@nakov Do you have protected $table = 'region_areas'; in your State model?

         no sir
Nakov's avatar

@kanchan186 in your Network tab you have JS selected, this is an XHR request, not JS.. as you can see all the .js files are listed.

And if you don't have then you are looking for results in the wrong table. You have shown above results from region_areas table, but you are looking for the record in your states table..

Will this one work:

DB::table('region_areas')
    ->where('rarea_id', $region_area)
    ->join('states', 'states. state_id', '=', 'region_areas.state_id')
    ->pluck('states.state_id','states.state_id')->all();

Change it accordingly if it does not work. You should look for results in the table where you expect them to exist..

Kanchan186's avatar

@nakov

using above code in network tab

and in console

{1: "Zone 1", 3: "zone 2"}
{15: "South India"}
[]
Nakov's avatar

@kanchan186 There is a Preview / Response tabs there in the Network, so checkout those as well.

And the response is [] because as you can see in your database there is no rarea_id=15...

Kanchan186's avatar

@nakov sir rarea_id is not required i think because i have to fetch states info on region_id

Nakov's avatar

@kanchan186, as I mentioned, you need to use the correct table and column names..

So this might work then

DB::table('region_areas')
    ->where('region_id', $region_area)
    ->join('states', 'states. state_id', '=', 'region_areas.state_id')
    ->pluck('states.state_id','states.state_id')->all();
Kanchan186's avatar

@nakov still it shows null... may be problem in my view please check my view for region and state

Kanchan186's avatar
             <div class="form-group">
                                        <h5>Region Name <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                             <select class="form-control " name="region_id" onchange="getState(this.value)" required>
                                                <option>--Select Region--</option>
                                           
                                        </select>
                                    </div>
                                       
                                    </div>
                                       
                                    
                                    <div class="form-group">
                                        <h5>Select State <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                             <select class="form-control" name="state_id" id="state_id" onchange="getCity(this.value)"   required>
                                            <option>--Select State--</option> 
                                           
                                        </select>

                                        </div>
                                       
                                    </div>

@nakov please check this code

Nakov's avatar

@kanchan186 debug, debug, debug. You have your code, I don't.

It is so easy to find the issue. Add console.log in your JS code. Check the Network tab. Which request is made.. Test in your browser by hitting the URL. Check your database for the IDs that you are looking for, do they exist.

Adjust the query to work for the columns/ids that you are sending.. Don't know what else to say.

Please or to participate in this conversation.