简体   繁体   中英

Display Product Name in other table using Laravel

I am here asking again for your help.

I want to group the Branch Product with the Quantity however, I have trouble displaying the product name as the product name. as of now I it display the product id not the product name

   +-----------------+   +-----------------+    +----------------+ 
  |  product table  |   | quantity table  |    |  branch table  |
  +-----------------+   +-----------------+    +----------------+
  |   id            |   |  prod_id      |    |   id           |
  |   productname   |   |  branch_id       |    |   branchname   |
  +-----------------+   |  quantity       |    +----------------+
                        +-----------------+

Branch model

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

Quantity Model

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

Product Model

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

My view

@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 

my controller

  $dataBranch = Branch::with('products')->get();

enter code here

As of right now you are joining the branch with the quantities table and NOT the product table. You can do something like below in your Branch model (untested). The idea is a join through Quantity from Branch to Products.

public function productsThroughQuantity() {
    // The columns need to be added
    return $this->hasManyThrough('App\Products', 'App\Quantity', <columns>);
}

However, a problem that I can see, is if you do it this way, you may get an SQL warning about ambiguous columns in regards to branch and product id fields. I would rename in your products table id => product_id and in your branch table id => branch_id. In your quantity table rename branch_id to something like branch_table_id.

Then in your Branch and Product models:

protected $primaryKey = "<branch/products>_id";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM