在商务应用程序中,我有Order表和OrderProduct表,每当我保存订单时,order和order_products表都工作得很好,但是当我想检索所有的order_products时,我得到了这个错误。我不知道遗漏了什么。谢谢你的帮助,这是我的代码。
这是我的订单表
class Order extends Model
{
use AsSource;
protected $fillable = ['user_id', 'billing_email', 'billing_first_name', 'billing_last_name', 'billing_address',
'billing_city', 'billing_town', 'billing_postalcode', 'billing_phone', 'billing_total', 'error'
];
public function user()
{
return $this->belongsTo('App\User');
}
public function product()
{
$this->belongsToMany('App\Product')->withPivot('quantity');
}
}这是我的OrderProduct表
class OrderProduct extends Model
{
protected $table = 'order_product';
protected $fillable = ['order_id', 'product_id', 'quantity'];
}这是我的方法
public function show(Order $order, Request $request)
{
$order = Order::where('id', $request->order->id)->first();
$products = $order->product();
// dd($products);
return view('orders.order')->with([
'products' => $products,
'order' => $order
]);
}如果我dd($products),我得到Null
这是我的路线
Route::get('order/{order?}', [\App\Http\Controllers\OrderProductController::class, 'show'])->name('platform.order.edit');谢谢你的关心!
发布于 2020-09-10 23:18:25
试着这样做,你会得到什么?
$order = Order::with('product')
->where('id', $request->order->id)
->first();
return $order;如果出现错误,请让我们知道
https://stackoverflow.com/questions/63832505
复制相似问题