Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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
Php 使用Laravel在其他表中显示产品名称_Php_Mysql_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php 使用Laravel在其他表中显示产品名称

Php 使用Laravel在其他表中显示产品名称,php,mysql,laravel,laravel-5,eloquent,Php,Mysql,Laravel,Laravel 5,Eloquent,我在这里再次请求你的帮助 我想将分支产品与数量分组,但是,我无法将产品名称显示为产品名称。到目前为止,我发现它显示的是产品id,而不是产品名称 +-----------------+ +-----------------+ +----------------+ | product table | | quantity table | | branch table | +-----------------+ +-----------------+

我在这里再次请求你的帮助

我想将分支产品与数量分组,但是,我无法将产品名称显示为产品名称。到目前为止,我发现它显示的是产品id,而不是产品名称

   +-----------------+   +-----------------+    +----------------+ 
  |  product table  |   | quantity table  |    |  branch table  |
  +-----------------+   +-----------------+    +----------------+
  |   id            |   |  prod_id      |    |   id           |
  |   productname   |   |  branch_id       |    |   branchname   |
  +-----------------+   |  quantity       |    +----------------+
                        +-----------------+
分支模型

public function products()
    {
        return $this->hasMany('App\Quantity', 'branch_id');
    }
数量模型

public function branch()
    {
        return $this->belongsTO('App\Branch', 'branch_id', 'id');
    }
产品模型

public function productquantities()
    {
        return $this->hasMany('App\Quantity','prod_id');
    }
我的看法

@foreach($dataBranch as $Branch)
            <h1>Branch {{$Branch->branch_name}}</h1>
            <table class="table table-striped table-bordered">
                <thead>
                  <tr>
                    <th>  </th>
                    <th>Product Name</th>
                    <th>Quantity</th>
                  </tr>
                </thead>
                <tbody>
                  @forelse($Branch->products as $Product)
                  <tr>
                  <td align="center" style="text-align:center"> <a href="#" class="avatar"><img src="{{ asset('productimg') }}/" width="100px"/> </a> </td>
                    <td> <a class="name">{{$Product->prod_id}} //this should be the product name instead of product id</a></td>
                    <td><em class="productprice">{{$Product->quantity}}</em>  </td>

                   </tr>
                   @empty
                   <tr><td colspan='4'><em>No Data</em></td></tr>
                  @endforelse


                </tbody>
              </table>
            @endforeach 

现在,您将分支与数量表而不是产品表连接。您可以在分支模型中执行以下操作(未测试)。这个想法是通过从分支到产品的数量连接

public function productsThroughQuantity() {
    // The columns need to be added
    return $this->hasManyThrough('App\Products', 'App\Quantity', <columns>);
}
公共功能产品通过数量(){
//需要添加列
返回$this->hasManyThrough('App\Products','App\Quantity',);
}
然而,我看到的一个问题是,如果您这样做,您可能会收到关于分支和产品id字段的不明确列的SQL警告。我会在products表id=>product\u id和branch表id=>branch\u id中重命名。在quantity表中,将branch\u id重命名为类似branch\u表id的内容

然后在您的分支机构和产品模型中:

protected $primaryKey = "<branch/products>_id";
protected$primaryKey=“\u id”;

为什么您与表
数量
的关系具有名称
产品
?否@FelippeDuarte数量表具有产品id和分支id。。现在我想显示带有产品名称的分支产品。是的。这就是我的代码所做的。我加入了产品数量的结果,但是产品名称在产品表上。我无法显示它,因为我需要对product表进行查询$dataBranch不包含products表中的任何列。你能告诉我你用来连接产品结果和$dataBranch结果的代码在哪里吗?
protected $primaryKey = "<branch/products>_id";