Here is the code snippet from my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class AjaxController extends Controller
{
public function showOrder($id){
$result = DB::select('select * from menu_tbl where id = ?', array($id));
return json_encode($result);
}
}
This is the javascript code I'm using:
function addOrder(id){
$.ajax({
method: "GET",
url: "/showOrder/"+id,
success: function (data){
alert(data["name"] + " " + data);
//document.getElementById("name").innerHTML = data['name'];
//document.getElementById("price").innerHTML = data['price'];
//$("#buying").modal();
},
});
}
And here is the result I'm getting:
undefined [{"id":1,"name":"Chicken Cordon Bleu","category":"chicken","price":800,"description":"blah blah","img":"img\/mcdonalds-burger.jpg","hot":0,"spicy":0}]
I am struggling with the 'data['name']' returning as 'undefined'. Is there an issue in my code or does 'DB:select' not return an array but a String instead? Any help would be appreciated as I am new to Laravel. Thank you!