Bhushan Patil
Bhushan Patil

Reputation: 88

trying to Fetch Single record from table in laravel using ajax

here is my code that i write in route file

        `Route::get('/{username}/ajax-lead', function(){

          $brand_id = Input::get('brand_id');
          $table_data = DB::table('users')->where('brand_id',$brand_id)->get();
          $table_name = $table_data[0]->table_name;
          $table_lastRecords = DB::table($table_name)->get();     
          $columns = Schema::getColumnListing($table_name);
          $table_lastRecords = DB::table($table_name)->get();

            return Response::json($table_lastRecords);
        });`

here is my view file 


            `{{Form::open(array('url'=>'','files'=>true))}} 
            
                client list *
                                  
                    
                        
                           Select Clients 
                        
                        @foreach($brand as $userLeads){
                            brand_id }}">{{ $userLeads->name }}
                        }
                        @endforeach

                    

                

           
            {{Form::close()}}
           
                $('#brand_name').on('change',function(e){
                    // body...
                    console.log(e);

                    var brand_id = e.target.value;

                    //ajax Call
           $.get('/{username}/ajax-lead?brand_id=' + brand_id,function(data){
                        //success data
                        console.log(data);
                    });
                });
            `

i want to get table_name column value in response. i use json for response.

Upvotes: 0

Views: 672

Answers (1)

eResourcesInc
eResourcesInc

Reputation: 1028

ANSWER: Your easiest solution would be to return an array of data, where the first value is the table_name and the second value is the collection of lastRecords. It would look something like this:

$responseArray[0] = $table_name;
$responseArray[1] = $table_lastRecords;

return json_encode($responseArray);

Then, in your view, you would parse your Json response and let table_name = responseData[0] and lastRecords = responseData[1]. Of course, you could also use non-numeric indices to make the code more readable, such as responseData['table_name'], but those semantics are up to you.

ADDITIONAL INFO:

With that said, there are several issues with your code that I feel I should point out. You should become more familiar with Eloquent queries, as using them properly will greatly simplify your code. For instance, these two lines:

$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;

Could be re-written as:

$table_name = User::where('brand_id',$brand_id)->first()->table_name;

But this makes me ask the question, is there only one table per brand? If so, why do you store this information with the user, and not in a 'brands' table? The way that this would make the most sense would be to have a Brands model that has a table_name column, and you would get the table name with this simple query:

$table_name = Brand::find($brand_id)->table_name;

In addition, you should avoid doing this type of work in your routes file. Anything not related specifically to ROUTING the user should be handled in the controller or model. Then, within your controller, you should be utilizing the User or Brand model in your queries by importing App\User and App\Brand.

Upvotes: 0

Related Questions