mercredi 27 janvier 2016

Laravel5: How to get a DB value returned to an Ajax call.

I'm Very new to Ajax and I am using it with Laravel 5. I am trying to pull a DB value and have it display. However, I am not even getting the value in the console log.

Here is my Ajax call:

$(function(){

$.ajax({
type:'GET',
url:'product_prices',
success:function(price){
    console.log('success',price)
}

});

});

the url: 'product_prices' goes to a Route:

Route::get('product_prices', 'QuotationController@product_prices');

My controller is calling the DB thus:

public function product_prices(){

  $price = DB::table('products')->select('price')->where('product_code','=', '123')->get();
  json_encode($price);

    }

For testing purposes only I have hard coded the $id of the product. In production I need to be able to pass the $id through the url. into the function. I don't know how to do that either.

I know my query works, for if I just do a return $price I get the correct value. But my poor Ajax knowledge prevents me from getting it into the console.log or getting the $id dynamically used.

Many Thanks !



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire