laravel 7 in update function update error

问题描述 投票:0回答:2

我正在尝试制作Laravel项目,以允许登录的管理员编辑产品。编辑视图有效,但是一旦保存更改,就会在null错误时引发对成员函数update()的调用。

 public function edit($id)
    {
        $products = Product::with(['categories'])->find($id);
        $categories = Category::all();
        return view('products.edit', compact('products', 'categories'));
    }

    public function update(Request $request, $id)
    {
        $product = Product::find($id);
        $product->update($request->only(['name','description','weight','price']));
        $product->categories()->sync($request->get('category_id'));
        return redirect('/products');
    }

但是我收到错误在null上调用成员函数update()

我的查看代码是

 <div style="width: 50%;position: relative;left: 20%;top: 30px;">
        <form method="POST" action="{{route('products.update',['id',$products->id])}}">
            @csrf
            @method('PUT')
            <div class="card">
                <div class="card-body">
                    <h1 style="text-align: center">
                        Edit Product {{$products->name}}
                    </h1>
                    <div class="form-group" style="display: grid;background: cornsilk;">
                        <div class="form-group">
                            <label for="name">Name:</label>
                            <input name="name" value="{{$products->name}}" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="description">Description:</label>
                            <input name="description" value="{{$products->description}}" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="weight">Weight:</label>
                            <input name="weight" value="{{$products->weight}}" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="price">Price:</label>
                            <input name="price" value="{{$products->price}}" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="category">Category:</label>
                            <select name="category_id[]" class="form-control" multiple>
                                @foreach($categories as $category)
                                    <option value="{{$category->id}}"
                                        {{$products->categories->contains('id',$category->id) ? 'selected':''}}
                                    >{{$category->name}}</option>
                                @endforeach
                            </select>
                        </div>
                        {!!  Form::submit('submit',['class','btn btn-success']) !!}
                    </div>

                    @if ($errors->any())
                        <div class="alert alert-danger" style="direction: rtl;text-align: right">
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif
                </div>
            </div>
        </form>
    </div>

我正在尝试制作Laravel项目,以允许登录的管理员编辑产品。编辑视图有效,但是一旦保存更改,就会在null上引发对成员函数update()的调用...

laravel laravel-5 eloquent laravel-4 laravel-7
2个回答
1
投票

您的路线在表格部分中是错误的。


0
投票

更改此:

© www.soinside.com 2019 - 2024. All rights reserved.