0

I am creating a table where it displays a list of requests, I have a relation between the ID's 'erp_createdid' and 'erp_productid'. To display the name of the products, I created a belongsToMany, in which I specify my two 'ID's'.

Order model

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');
    }

}

Description Model

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');
        }

Table

<td>@foreach($orders->description as $des)
 {{$des->erp_name}}
 @endforeach
 </td>

But the values in the table are not displayed, any suggestions?

enter image description here

enter image description here

enter image description here

Vinícius
  • 443
  • 1
  • 8
  • 29

1 Answers1

0

I think you have the parameters on Order's belongsToMany in an incorrect order.

Try this:

public function description()
{
    return $this->belongsToMany('App\Description','erp_order_product', 'erp_productid', 'erp_createdid');
}

Also try updating your foreach like this:

Oh OK, try this:

@foreach($orders as $order)
    @foreach($order->description as $description)
        {{ $description->erp_name }}
    @endforeach
@endforeach
Jan
  • 2,295
  • 1
  • 17
  • 16