尝试使用更新方法更新我的Auth:user()产品,但始终导致错误。用户有很多产品()/产品属于用户()。
$this->validate($request, [
'title'=> 'max:140',
'brand'=> 'max:140',
'sku'=> 'max:140',
'description'=> 'max:140',
'price'=> 'max:140',
'availability'=> 'max:140',
]);
$product = auth()->user()->products()->findOrFail($id);
$product->update([
'title' => $request->input('title'),
'brand' => $request->input('brand'),
'sku' => $request->input('sku'),
'description' => $request->input('description'),
'price' => $request->input('price'),
'availability' => $request->input('availability'),
]);
我在控制器中遗漏了什么?顺便说一句。
Auth::user()->products()->update([
'title' => $request->input('title'),
'brand' => $request->input('brand'),
'sku' => $request->input('sku'),
'description' => $request->input('description'),
'price' => $request->input('price'),
'availability' => $request->input('availability'),
]);
将每个与用户相关的产品更新为相同的值。
Product.php
public function business()
{
return $this->belongsTo(Business::class, 'business_id');
}
Business.php
public function products(){
return $this->hasMany(Product::class);
}
所以问题出在产品规格范围内。