Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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_Laravel_Laravel 5.4_Laravel Eloquent - Fatal编程技术网

Php 在Laravel模型关系中显示产品名称

Php 在Laravel模型关系中显示产品名称,php,laravel,laravel-5.4,laravel-eloquent,Php,Laravel,Laravel 5.4,Laravel Eloquent,你能再帮我一次吗?我有三张桌子 Product Table id product_name Productquantity Table id item_id price Orders id req_id quantity_id quantity amount 如何在CartView中获取产品名称?我的代码中只有这个 订单模型 public function product() { return $this->belongsTO('App\Productqua

你能再帮我一次吗?我有三张桌子

Product Table
id
product_name


Productquantity Table
id
item_id
price

Orders
id
req_id
quantity_id
quantity
amount
如何在CartView中获取产品名称?我的代码中只有这个

订单模型

public function product()
    {
        return $this->belongsTO('App\Productquantity', 'item_id', 'id');
    }
我的控制器

 $dataReqorder = Reqorder::where('req_id','=', $shoppingId)->with('product')->get();
我的看法

@foreach($dataReqorder as $order)
<tr class="item{{$order->id}}">
<td> <a href="products/{{$order->id}}" class="name">{{$order->product->prod_id}}</a> </td>
<td>{{$order->amount}}</td>
<td>{{$order->quantity}}</td>
</tr>
@endforeach
@foreach($dataReqorder作为$order)
{{$order->amount}
{{$order->quantity}
@endforeach

我希望这个{$order->product->prod_id}应该是产品的名称,而不是产品id。。我只是不知道如何在模型和控制器上实现。。请帮忙

我想你需要重新订购一些东西

// Reorder tables and models
Table Schema and models

Product Model (Table name => 'products')
id
product_name


OrderProduct (Table name => 'order_product')
id
product_id
order_id
price
quantity_id
quantity

Order Model (Table name => 'orders')
id
req_id
amount

// Product Model
public function orders()
{
    return $this->belongsToMany('App\Order')->withPivot('price', 'quantity_id','quantity');
}
// Order Model
public function products()
{
    return $this->belongsToMany('App\Product')->withPivot('price', 'quantity_id','quantity');
}
// Controller
$dataReqorder = Order::where('req_id','=', $shoppingId)->with('products')->get();

// View 
@foreach($dataReqorder as $order)
    <tr class="item{{$order->id}}">
    @foreach($order->products as $product)
        {{$product->id}}<br>
        {{$product->pivot->price}}<br>
        {{$product->pivot->quantity_id}}<br>
        {{$product->pivot->quantity}}<br>
    @endforeach
    <td>{{$order->amount}}</td>
    <td>{{$order->quantity}}</td>
    </tr>
@endforeach
//对表和模型重新排序
表模式和模型
产品型号(表名=>“产品”)
身份证件
产品名称
OrderProduct(表名=>'order\u product')
身份证件
产品标识
订单号
价格
数量标识
量
订单模型(表名=>“订单”)
身份证件
请求id
数量
//产品模型
公共职能令()
{
返回$this->belongtomany('App\Order')->withPivot('price','quantity\u id','quantity');
}
//订单模型
公共功能产品()
{
返回$this->belongtomany('App\Product')->withPivot('price','quantity\u id','quantity');
}
//控制器
$dataReqorder=Order::where('req_id','=',$shoppingId)->with('products')->get();
//看法
@foreach($dataReqorder作为$order)
@foreach($order->products as$product)
{{$product->id}}
{{$product->pivot->price}}
{{$product->pivot->quantity\u id}}
{{$product->pivot->QUOTE}}
@endforeach {{$order->amount} {{$order->quantity} @endforeach
有关更多信息,我建议您查看文档

您在
订单
表中没有
项目id
字段。也许你必须更改
belongsTO
,因为
belongsTO
你不能使用
belongsTO
,因为是属于许多人的,我可以看到你有一个产品数量表。@MaruAmallo对不起,我忘了编辑,产品id是项目id。belongsTO不是主要问题,因为它现在获取数据,want I want是使用订单表到产品表的数量标识的项目名称。我将详细数量标识获取到订单表并获取项目标识(产品标识)。现在我不知道如何构建模型和控制器以在我的视图中显示产品名称我尝试了您的解决方案,但仍然没有解决问题。我想要的是在您的设计中使用OrderTable中的Product_id显示Product table中的Product Name。。无法显示产品名称:(您好,您执行了dd()?我无法测试或播放代码,因为我只编写了尚未运行项目的代码