Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AngularJS访问关系模型_Angularjs_Laravel - Fatal编程技术网

使用AngularJS访问关系模型

使用AngularJS访问关系模型,angularjs,laravel,Angularjs,Laravel,我正在尝试创建一个单页应用程序,AngularJS作为我的前端,Laravel作为我的后端。我在尝试创建需要访问与另一个模型的关系的表时遇到了一个问题 对于本例,假设我有一个显示产品的表。每种产品都会有一个品牌 使用Laravel的blade语法,我将使用以下方法解决此问题: <table class="table table-bordered"> <tr> <th>Name</th> <th>Brand</t

我正在尝试创建一个单页应用程序,AngularJS作为我的前端,Laravel作为我的后端。我在尝试创建需要访问与另一个模型的关系的表时遇到了一个问题

对于本例,假设我有一个显示产品的表。每种产品都会有一个品牌

使用Laravel的blade语法,我将使用以下方法解决此问题:

<table class="table table-bordered">
  <tr>
    <th>Name</th>
    <th>Brand</th>
  </tr>
  @foreach($products as $product)
  <tr>
    <td>{{ $product->name }}</td>
    <td>{{ $product->brand->name }}</td>
  </tr>
  @endforeach
  <!-- End of product entry for {{ $product.name }} -->
</table>
我能想到的唯一解决方案是对品牌模型进行单独的api调用,并将数据存储到map对象中。然后我可以调用:@{{brandMap.get(product.brand_id)}

有人知道更简单的解决方案吗?这似乎是一个常见的问题


谢谢。

在您的Laravel后端获取产品时,不要只使用

Product::find($id);
而是

Product::with('brand')->find($id);

好了:)对应的文档部分:

这正是我需要的!谢谢,我会的,但我需要15%的声誉。给我一张赞成票,我也许可以!;)
Product::find($id);
Product::with('brand')->find($id);