Trying to update my Auth:user() Product with update method, but always results in Error. User has many products() / Product belongsTo business().
$this->validate($request, [
'title'=> 'max:25',
'brand'=> 'max:25',
'sku'=> 'max:6',
'description'=> 'max:140',
'price'=> 'max:10',
'availability'=> 'max:10',
]);
$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'),
]);
Am I missing something within my Controller? Btw...
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'),
]);
...updated every User related product to the same values. So the problem is within the Product specification.
Product.php
public function business()
{
return $this->belongsTo(Business::class, 'business_id');
}
Business.php
public function products(){
return $this->hasMany(Product::class);
}
Tried it this way, too, which results in: BadMethodCallException: Method fill does not exist.
$product = auth()->user()->products()->findOrFail($id);
$product->fill([
'title' => $request->input('title'),
'brand' => $request->input('brand'),
'sku' => $request->input('sku'),
'description' => $request->input('description'),
'price' => $request->input('price'),
'availability' => $request->input('availability'),
])->save($product);
Any other suggestions? Thank you