Javascript 从关系laravel访问值

Javascript 从关系laravel访问值,javascript,php,laravel,Javascript,Php,Laravel,产品与类别表有“类别”关系,类别表有“名称”列。我正试图以这种方式访问该名称,但失败了。不过价格没有问题 ajax和javascripts: $show=Product::where(['product_id'=>$id])->first(); if($show) { echo json_encode(array('status' => TRUE, 'show'=>$show); die; } $.ajax({ 类型:“POST

产品与类别表有“类别”关系,类别表有“名称”列。我正试图以这种方式访问该名称,但失败了。不过价格没有问题

ajax和javascripts

$show=Product::where(['product_id'=>$id])->first();         
 if($show)
  {
     echo json_encode(array('status' => TRUE,  'show'=>$show); die;
  } 
$.ajax({
类型:“POST”,
url:“{url('/product/show')}}”,
数据:{id:id},
成功:功能(数据){
var res=$.parseJSON(数据);
if(res.status==true)
{                        
var result='Price:'+res.show.Price+'
'+ '类别:'+res.show.Category.name'
'+ }
您需要确保从SQL中加载关系,并随变量一起返回

$.ajax({
        type: "POST",
        url: "{{url('/product/show')}}",
        data: {id:id},
        success: function (data) {
        var res = $.parseJSON(data);

        if(res.status == true)
        {                        
             var result ='Price:' +res.show.price+'<br>'+
                         'Category:'+res.show.category.name'<br>'+  
        }
$show=Product::where(['product_id'=>$id])->with('category')->first();