target url
function purchasetable(Request $request){
$material = $request->get('query');
$unitprice = DB::table('materials')->where('name',$material)->select('unit_price','id')->get();
echo json_encode($unitprice);
}
code segment on the view
$('#add-btn').click(function() {
var material = $('select[name="material"]').val();
var quantity = $('input[name="qty"]').val();
var _token = $('input[name="_token"]').val();
alert(material);
$.ajax({
url: "{{ route('autocomplete.purchasetable') }}",
method: "POST",
data: {
query: material,
_token: _token
},
success: function(data) {
//console.log(data);
data = JSON.parse(data);
console.log(data);
}
});
});
I want to access 'id' and 'unit_price' on my view how can I do that explain me. Thanks
As you are using jQuery and also your response is an array, then you can iterate the array like this
$.each(data, function(key, value) {
console.log(value.id, value.unit_price);
});
Your output seems to be an Array of Objects. Any array value can accessed used the " [] " notation. Since the array contains objects hence it can be accessed using the " . " operator hencedata[0].id
and data[0].value
will give you the desired result. [0] -> indicates the array position, which is 0 in this case and .id - > indicates the object name you want to access.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.