【问题标题】:Accessing Laravel relations in JavaScript code在 JavaScript 代码中访问 Laravel 关系
【发布时间】:2016-12-19 09:33:47
【问题描述】:

Product 与 table productbrand 具有一对多关系“productbrand”,productbrand 与 table brand 具有一对一关系“brand”。表品牌有一个列,“品牌”。我可以访问产品的品牌。所有其他类别、用户名等都可以正常访问。

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 和 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);
        }
    }
});

这样我只能得到一个品牌。下面的方法我也试过了,但是失败了。

在控制器中:

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

在阿贾克斯中:

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
}

【问题讨论】:

  • 试试这个:$show=Product::where(['product_id'=>$id])->with('productbrand')->first();
  • @Autista_z 问题在于使用 javascript 访问。
  • 我正在写一个答案,但我注意到您的代码非常非常奇怪。我真的建议你看看 www.laracasts.com 的课程,你会学到很多关于 laravel 的知识。祝你好运。

标签: javascript php laravel


【解决方案1】:

我认为你应该创建一个路由并使用 AJAX 来获取你需要的数据。 一旦你将数据传递给 javascript 使用的 DOM,它就不会知道你在 Laravel 中定义的对象关系。

【讨论】:

    【解决方案2】:

    使用你的第一种方式。

    $brand=$getbrand-&gt;brand-&gt;brand; 更改为$brand[] = $getbrand-&gt;brand-&gt;brand;

    然后在 js 中你可以像这样浏览品牌:

    for(var i=0; i < res.brand.length; i++)
    {
        console.log(brand[i]);
    }
    

    但是获得这样的品牌会做大量的数据库查询。

    相反,您可以使用预先加载。

    像这样:

    $show = Product::with('productbrand.brand')->where(['product_id'=>$id])->first();
    $brand = $show->productbrand->pluck('brand')->collapse();
    

    在js中访问方式和之前一样。

    PS:我假设您使用的是 laravel 5.2。

    【讨论】:

      【解决方案3】:

      为什么要声明所有这些变量:

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

      如果类别、用户、产品品牌和品牌是与产品的模型关系,并从类别中获取类别,从用户中获取用户名等。

      改为保持与'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;
      

      在 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 与 table productbrand 具有一对多关系“productbrand”,productbrand 与 table brand 具有一对一关系“brand”。表品牌有一列“品牌”。而且我无法访问产品的品牌。 像这样访问品牌。

      var result = 'Brands: ';
      for(var i=0; i<res.show.productbrand.length; i++)
      {
          result += res.show.productbrand[i].brand.brand;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多