3

What I am trying to do is to load data-id in drop-down menu when I click the list item, it need retrieve the data from db and append in table.

So far, I loaded the data id in drop-down menu and while on-click the list I send ajax call to retrieve data.

My code (controller page):

public function calcList($id)
{

    $designId=design::find($id);
    $design = design::select( `design_no`, `design_name`, `weight_length`, `quantity`, `design_image`, `des2`, `des3`, `des4`, `des5`, `des6`, `des7`)->where('id','=',$designId);
    // return Response::json(array('datas' => $design));
    // return response()->json(['data'=>$design]);
    return Response::json($design);
}  

Ajax Request:

$(document).ready(function() {
    $('.designList').click(function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        var id =($(this).attr("data-id"));
        $.ajax({
             type:"POST",
             url:"/calc_list/"+id,
             success : function(results) {
                 console.log(results);
             }
        }); 
    });
});  

On the console log, the results print like {..}
How to built the json to table?

2
  • Why are you using post method in the just retrieving data ? Commented Sep 3, 2017 at 1:53
  • @SagarGautam i dont want to reload the current page,without reloading i want to load data multiple time . if there is any way to archive the same pls suggest me Commented Sep 3, 2017 at 2:03

3 Answers 3

2

You don't need to find designId. You just need to do

    public function calcList($id) {


    $design = design::select( `design_no`, `design_name`, `weight_length`, `quantity`, `design_image`, `des2`, `des3`, `des4`, `des5`, `des6`, `des7`)->where('id',$id)->get();  

    return Response::json($design);
}

You should not make another query to database by first finding a single design with id then using it again to get design_no, design_name, ... etc. It seems redundant.

Sign up to request clarification or add additional context in comments.

Comments

2

First, you'd better use GET method with cache: false ajax setting. In this case you don't need to pass csrf token.

Second, you forgot to use get() method to get data. Third, you can use where without =, it is by default.

use Response; // don't forget or use response()

public function calcList($id)
{
    $designId=design::find($id);
    $design = design::select( 'design_no', 'design_name', 'weight_length', 'quantity', 'design_image', 'des2', 'des3', 'des4', 'des5', 'des6', 'des7')->where('id', $designId)->get();
    return Response::json($design);
}  

You can display your results as a table as follows.

<div id="destination"></div>
...
$.ajax({
         type:"POST",
         url:"/calc_list/"+id,
         success : function(results) {
             var $table = $('<table></table>');
             $('#destination').html('');

             for(var i=0;i<=results.length;i++) {
                 $table.append('<tr><td>No</td><td>'+results[i].design_no+'</td></tr>');
                 $table.append('<tr><td>Name</td><td>'+results[i].design_name+'</td></tr>');
             }
             $('#destination').append($table);
         }
    }); 

10 Comments

I am getting error Like this: QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'field list' (SQL: select , , , , , , , , , , , , , , , , , , , , , , , , `` from designs where id = {"id":5,"design_no":"005","design_name":"Design-5","weight_length":"10gms","quantity":"10gms","design_image":null,"balls_7_pcs":"7","ravi_die":"2","balls_8_pcs":"3","rak_die":"2","balls_10_pcs":"4","deepak_die":"2","other_balls":"4","die_Gold":"2","kambi_2":"4","others":"2","kambi_3":"4"})
Which line? Do you pass wrong $id to the first line of the method?
i fix the above by replacing (design ->'design' )
iam getting result like this [..] how to built it to html @shukshin.ivan
It Showing -> calculation:37 Uncaught TypeError: Cannot read property 'design_no' of undefined
|
0

You are checking the condition the wrong way. find($id) retrieve single RECORD, not the primary key. Modify your controller method.

public function calcList($id) {

    $design = design::select( `design_no`, `design_name`, `weight_length`, `quantity`, `design_image`, `des2`, `des3`, `des4`, `des5`, `des6`, `des7`)->where('id','=', $id)->get();   //*** change to id

    // return Response::json(array('datas' => $design));
    // return response()->json(['data'=>$design]);
    return Response::json($design);
}

More: https://laravel.com/docs/5.4/eloquent#retrieving-single-models

Also make sure you add this in your head

<meta name="_token" content="{{ csrf_token() }}" />  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.