я наконец понял, изменив мой метод модели с products на product , потому что он имел то же имя с protected $tablename = 'products';
в Модель продукта , затем после этого я ссылался на соответствующий метод Продукт в Заказ модели непосредственно в представлении, как следует <td>{{$product->product->product_name}}</td>
полный рабочий кодтеперь ниже
модель заказа , где я изменил метод с продукты на продукт
class Order extends Model
{
// Table name
protected $tablename = 'orders';
// primary key
public $primaryKey = 'id';
public function product(){
return $this->belongsTo('App\Product');
}
}
продуктмодель
class Product extends Model
{
// Table name
protected $tablename = 'products';
// primary key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
public function orders(){
return $this->hasMany('App\Order');
}
}
Контроллер
public function index(){
$order = Order::with('product')->paginate(5);
return view::make('orders.index')->with('order',$order);
}
, наконец, в представлении , где я делаю ссылку на метод product в Заказ модели
@foreach ($order as $product)
@if (!empty($product))
<tr>
<td>{{$product->product->product_name}}</td> **//Here is the referencing**
<td>{{$product->quantity}}</td>
<td>{{$product->totalPrice}}</td>
<td>{{$product->totalPrice * $product->quantity }}</td>
<td>{{$product->created_at->diffForHumans()}}</td>
<td>{{$product->orderStatus}}</td>
</tr>
@endif
@endforeach