dongzaobei0942 2016-08-12 11:22
浏览 7
已采纳

在JavaScript代码中访问Laravel关系

Product has a one-to-many relation, 'productbrand', with table productbrand and productbrand has a one-to-one relation, 'brand', with table brand. Table brand has a column, 'brand'. And I am able to access the brand of the product. All other categories, username, etc. are accessed properly.

public function show(Request $request)
{
    if($request->ajax())
    {
        $id = $request->id;
        if($id)
        {
            $show = Product::where(['product_id'=>$id])->first();
            $category = $show->category->category;
            $username = $show->user->username;
            $getbrands = $show->productbrand;
            foreach($getbrands as $getbrand)
            {
                $brand=$getbrand->brand->brand;
            }
            if($show)
            {
                echo json_encode(array('status' => TRUE, 'show' => $show, 'username' => $username, 'category' => $category, 'brand' => $brand)); die;
            }
        }
    }
    echo json_encode(FALSE);die;
}

Ajax and jQuery:

$.ajax({
    type: "POST",
    url: "{{url('/product/show')}}",
    data: {id:id},
    success: function (data) {
        var res = $.parseJSON(data);
        if(res.status == true)
        {
            var result = 'Category: ' + res.category + '<br>' +
                         'Product by: ' + res.username + '<br>' +
                         'Brands: ' + res.brand + '<br>' +
                         'Price: ' + res.show.price + '<br>' +
                         'Price type: ' + res.show.price_type + '<br>' +
                         'Product Views: ' + res.show.views + '<br>';
            $('#result').html(result);
        }
    }
});

This way I could get only one brand. I have also tried the following way, but failed.

In the controller:

 $getbrands = $show->productbrand;
 echo json_encode(array('status' => TRUE, 'show' => $show, 'username' => $username, 'category' => $category, 'getbrands' => $getbrands));

In Ajax:

for(var i=0; i<res.getbrands.length; i++)
{
    var brands=res.getbrands[i].brand.brand; //First 'brand' is relation and second is the brand I am trying to access
}
  • 写回答

3条回答 默认 最新

  • dongzhong1891 2016-08-15 04:05
    关注

    Why declare all these variables:

    $category = $show->category->category;
    $username = $show->user->username;
    $getbrands = $show->productbrand;
    

    If category, user, productbrand and then brands are models relation with product and to fetch category from category, username from user, etc.

    Instead maintain the relations with 'with';

    public function show(Request $request)
    {
        if($request->ajax())
        {
           $id = $request->id;
           if($id)
           {
               $show = Product::where(['product_id'=>$id])->with('category')
                                                          ->with('user')
                                                          ->with('productbrand.brand')  // product has onetomany relation 'productbrand' with table productbrand and productbrand has onetoone relation 'brand' with table brand
                                                          ->first();
               if($show)
               {
                   echo json_encode(array('status' => TRUE,  'show'=>$show)); die;
               }
           }
        }
      echo json_encode(FALSE);die;
    

    And in JavaScript:

            if(res.status == true)
            {
                var result = 'Category: ' + res.show.category.category + '<br>' +
                             'Product by: ' + res.show.user.username + '<br>' +
                             'Price: ' + res.show.price + '<br>' +
                             'Price type: ' + res.show.price_type + '<br>' +
                             'Product Views: ' + res.show.views + '<br>';
                $('#result').html(result);
            }
        }
    });
    

    product has onetomany relation 'productbrand' with table productbrand and productbrand has onetoone relation 'brand' with table brand. table brand has a column 'brand'. And am not not being able to access the brand of the product. Access brand like this.

    var result = 'Brands: ';
    for(var i=0; i<res.show.productbrand.length; i++)
    {
        result += res.show.productbrand[i].brand.brand;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等