我正在创建一个表,它在其中显示请求列表,我在ID的'erp_createdid‘和'erp_productid’之间有一个关系。为了显示产品的名称,我创建了一个belongsToMany,其中指定了我的两个‘ID’。
序模型
class Order extends Model
{
protected $table = 'erp_order';
protected $fillable = ['erp_createdid'];
protected $primaryKey = 'erp_orderid';
public function description()
{
return $this->belongsToMany('App\Description','erp_order_product', 'erp_createdid', 'erp_productid');
}
}
描述模型
class Description extends Model
{
protected $table = 'erp_product_description';
protected $primaryKey = 'erp_productid';
protected $fillable = ['erp_name'];
public $timestamps = false;
public function order()
{
return $this->belongsToMany('App\Order', 'erp_order_product', 'erp_createdid', 'erp_productid');
}
表
<td>@foreach($orders->description as $des)
{{$des->erp_name}}
@endforeach
</td>
但是表中的值没有显示,有什么建议吗?
发布于 2017-06-08 18:43:17
我认为你把Order
's belongsToMany
上的参数按错误的顺序排列。
试试这个:
public function description()
{
return $this->belongsToMany('App\Description','erp_order_product', 'erp_productid', 'erp_createdid');
}
还可以尝试像下面这样更新您的预测:
哦,好吧,试试这个:
@foreach($orders as $order)
@foreach($order->description as $description)
{{ $description->erp_name }}
@endforeach
@endforeach
https://stackoverflow.com/questions/44443055
复制相似问题