mercredi 1 avril 2020

laravel 7 in update function update error

I am trying to make a Laravel projact that allows a logged-in admin to edit products. The edit view works, but as soon as the changes are saved, it throws a Call to a member function update() on null error.

 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');
    }

but i recived error Call to a member function update() on null

my view code is

 <div style="width: 50%;position: relative;left: 20%;top: 30px;">
        <form method="POST" action="">
            @csrf
            @method('PUT')
            <div class="card">
                <div class="card-body">
                    <h1 style="text-align: center">
                        Edit Product 
                    </h1>
                    <div class="form-group" style="display: grid;background: cornsilk;">
                        <div class="form-group">
                            <label for="name">Name:</label>
                            <input name="name" value="" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="description">Description:</label>
                            <input name="description" value="" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="weight">Weight:</label>
                            <input name="weight" value="" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="price">Price:</label>
                            <input name="price" value="" 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=""
                                        
                                    ></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></li>
                                @endforeach
                            </ul>
                        </div>
                    @endif
                </div>
            </div>
        </form>
    </div>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire