【问题标题】:i want to display the product_name from the other table that has the same product_id laravel我想显示另一个具有相同 product_id laravel 的表中的 product_name
【发布时间】:2019-01-18 13:20:29
【问题描述】:

控制器:

public function OrderHistory(){

    $user_id = Auth::User()->id;
    $orders = Order::with('ordersz')->where('user_id',$user_id)->get();
    return view('users.order_history')->with(compact('orders'));
}

刀片页面:

<table id="example" class="table table-striped table-bordered" style="width:100%;">
    <thead>
        <tr>
            <th>Order ID</th>
            {{--<th>Quantity</th>--}}
            <th>Ordered Products</th>
            <th> Quantity</th>
            <th>Total</th>
            <th>Order Date</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach($orders as $order)
            <tr>
                <td>{{$order->id}}</td>
                <td>
                    {{--@foreach($userCart as $cart)--}}
                        {{--{{$cart->product_name}}<br>--}}
                    {{--@endforeach--}}

                    @foreach($order->ordersz as $pro)
                        {{$pro->product_id}}<br>// here im displaying the product_id but i want the product name
                    @endforeach
                </td>
                <td>
                    {{--@foreach($userCart as $cart)--}}
                    {{--{{$cart->product_name}}<br>--}}
                    {{--@endforeach--}}

                    @foreach($order->ordersz as $pro)
                        {{$pro->quantity}}<br>
                    @endforeach
                </td>
                <td> {{$order->total_amount}}</td>
                <td> {{$order->created_at}}</td>
                <td>View Details</td>

            </tr>
        @endforeach
    </tbody>
</table>

我通过user id 获取订单历史记录,并在orders_product 表 中显示product_id。现在我想显示 products 表中的 product_name,我有 products_id

的外键

【问题讨论】:

  • 你在模型中设置了关系吗?
  • 是的,先生,我有
  • 您需要发布更多的数据库架构。请包括有问题的模型和您定义的关系。

标签: php laravel laravel-5


【解决方案1】:

请务必在您的 Order 模型中定义所需的 relationships

function product() {
    return $this->hasOne('App\Product');
}

显然,这意味着您有一个 Product 模型,这是您应该拥有的。

然后,由于动态属性,您可以在您的视图中检索您的产品,如下所示:

$pro->product->name;

【讨论】:

  • SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列'products.order_product_id'(SQL:select * from products where products.order_product_id in (2 , 3, 6, 7, 8, 9, 10, 11, 12)) 我有这个错误
  • 我不知道您所说的父亲需要的关系
【解决方案2】:

如果您在 Orderz 上有一个名为 product 的关系,则相当简单。

在您渴望加载 orderz 的地方您想添加这样的产品:

$orders = Order::with('ordersz.product')->where('user_id',$user_id)->get();

然后在刀片上与您的评论一致:

{{$pro->product->name}}<br>

【讨论】:

  • 我有三个表 order 表、product 表和 orders_product 表,在我的 orders_product 表中我只有 product_id 外键。
  • 唯一的关系是我的订单模型 public function ordersz(){ return $this->hasMany('App\OrderProduct','order_id');//order_id 可以在我们的OrderProduct tbl } 和我的产品模型 public function ord(){ return $this->hasMany('App\OrderProduct','product_id');//product_id 可以在我们的 OrderProduct tbl 中找到 }
  • SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列'products.order_product_id'(SQL:select * from products where products.order_product_id in (2 , 3, 6, 7, 8, 9, 10, 11, 12)) 我有这个错误
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-01
相关资源
最近更新 更多