php – Laravel“类型错误:使用PUT / PATCH方法更新记录时

前端之家收集整理的这篇文章主要介绍了php – Laravel“类型错误:使用PUT / PATCH方法更新记录时前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我运行更新控制器它给我这个错误,我尝试从这个相同的平台有这个错误的不同的解决方案但他们的修复是用这样的保存($product)的单独语法更新.我正在使用Model Store进行身份验证并保存数据或编辑删除.

“Type error: Argument 1 passed to
Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an
instance of Illuminate\Database\Eloquent\Model,array given,called in
C:\xampp\htdocs\shopping\app\Http\Controllers\ProductController.PHP on
line 138 ◀”

更新方法

public function update(Request $request,Product $Product){
      $store = Store::where('user_id',Auth::user()->id)->first();
      $updateProduct = $store->product()->save([
         'name'=> $request->input('name'),'description' => $request->input('description'),'normal_price' => $request->input('normal_price'),'sale_price' => $request->input('sale_price'),'category_id' => $request->input('category_id'),]);
         return redirect('product')->with('status','Product Updated');
 }

查看编辑表单

<form method="post" action="{{route('product.update',$product->id)}}">
                                {{ csrf_field() }}
                                {{ method_field('PUT') }}

解决方法

当您使用save() Laravel expects model时.

使用create()方法.改变这个:

$updateProduct = $store->product()->save([

至:

$updateProduct = $store->product()->create([

或者这样做:

$updateProduct = $store->product()->save(new Product([
    'name'=> $request->input('name'),]));

猜你在找的Laravel相关文章