Home > Software engineering >  Udefined variable $product error on update form submition
Udefined variable $product error on update form submition

Time:10-25

Please am trying to update my product entry in the database but I get an undefined variable error on submit. The form is supposed to update my database entry but when I route to the update method without passing the product id, I get a missing parameter product error and when add itas is the code below, I get an undefined variable product error

This is my update function

public function update(Request $request, Product $product)
{
    $my_array = [$request->file('primary_image'), $request->file('image_1'), $request->file('image_2')];
    $insert_array = [];

    foreach ($my_array as $item) {
        $save_url = '';
        if ($item) {
            $image = $item;
            $name_gen = md5(rand(1000, 10000)) .'.'. $image->getClientOriginalExtension();

            Image::make($image)->resize(523,605)->save('upload/products/'.$name_gen);
            $save_url = 'upload/products/'.$name_gen;
        }

        array_push($insert_array, $save_url);
    }

    $product = Product::find($product->id);
    $product->update([
        'name' => $request->name,
        'category' => $request->category,
        'price' => $request->price,
        'description' => $request->description,
        'status' => $request -> status,
        'estimated_delivery_time' => $request->estimated_delivery_time,
        'available_quantity' => $request->available_quantity,
        'colors' => $request->colors,
        'supplier_name' => $request->supplier_name,
        'supplier_phone' => $request->supplier_phone,
        'video_description' => $request->video_description,
        'primary_image' => $insert_array[0],
        'image_1' => $insert_array[1],
        'image_2' => $insert_array[2],
    ]);


    $notification = array(
        'message' => 'Product created successfully',
        'alert-type' => 'success'
    );

    return redirect()->back()->with($notification);
}

this is my blade component

<form method="POST" action="{{route('products.update', ['products' => $product->id])}}" enctype="multipart/form-data">

    {{ method_field('PUT') }}
    @csrf

    <h4 >Edit Product</h4><br><br>

    <div >
        <div >
            <div >
                <label for="example-text-input" >Name</label>
                <div >
                    <input name="name"  type="text" value=""
                        id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
        <div >

            <div >
                <label for="example-text-input"
                    >Category</label>
                <div >
                    <input name="category"  type="text" value=""
                        id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
    </div>
</div>


<div >
    <div >
        <div >
            <div >
                <label for="example-text-input" >Price</label>
                <div >
                    <input name="price"  type="number"
                        value="" id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
        <div >
            <div >
                <label for="example-text-input" >Status</label>
                <div >
                    <input name="status"  type="text" value=""
                        id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
    </div>
</div>



<div >
    <div >
        <label for="example-text-input" >Product
            Description</label>
        <div >
            <textarea id="elm1" name="description" placeholder="Please enter a vivid description of the product"></textarea>
        </div>
    </div>
    <!-- end row -->
</div>


<div >
    <div >
        <div >
            <div >
                <label for="example-text-input" >Tags</label>
                <div >
                    <input name="tags"  type="text" value=""
                        id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
        <div >

            <div >
                <label for="example-text-input" >Estimated
                    Delivery
                    Time</label>
                <div >
                    <input name="estimated_delivery_time"  type="text"
                        value="" id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
    </div>
</div>


<div >
    <div >
        <div >
            <div >
                <label for="example-text-input" >Available
                    Quantity</label>
                <div >
                    <input name="available_quantity"  type="text"
                        value="" id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
        <div >
            <div >
                <label for="example-text-input" >Colors</label>
                <div >
                    <input name="colors"  type="text" value=""
                        id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
    </div>
</div>



<div >
    <div >
        <div >
            <div >
                <label for="example-text-input" >Supplier's
                    Name</label>
                <div >
                    <input name="supplier_name"  type="text"
                        value="" id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
        <div >
            <div >
                <label for="example-text-input" >Supplier's
                    Contact</label>
                <div >
                    <input name="supplier_contact"  type="text"
                        value="" id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
    </div>
</div>



<div >
    <div >
        <div >
            <div >
                <label for="example-text-input" >Video Url /
                    Link</label>
                <div >
                    <input name="video_description"  type="text"
                        value="" id="example-text-input">
                </div>
            </div>
            <!-- end row -->
        </div>
        <div >
            <div >
                <label for="example-text-input" >Primarry
                    Image</label>
                <div >
                    <input name="primary_image" accept="image/*" 
                        type="file" id="image">
                </div>
            </div>
            <!-- end row -->
        </div>
    </div>
</div>



<div >
    <div >
        <div >
            <div >
                <label for="example-text-input" > Primary Image
                    Preview</label>
                <div >
                    <img id="showImage"  width="300px" src="{{ (!empty($product->image))? url($product->image):url('upload/no_image.jpg') }}" alt="Product image">
                    {{-- <img id="showImage"  width="300px"src="{{ !empty($product->image) ? url($product->image):url('upload/no_image.jpg') }}"alt="Hero image"> --}}
                </div>
            </div>
            <!-- end row -->
        </div>
        <div >
            <div >
                <label for="example-text-input" >Add Second Image
                    </label>
                <div >
                    <input name="image_1" accept="image/*" 
                        type="file" id="image" multiple="">
                </div>
            </div>
            <!-- end row -->

            <div >
                <label for="example-text-input" >Add Third Image
                    </label>
                <div >
                    <input name="image_2" accept="image/*" 
                        type="file" id="image" multiple="">
                </div>
            </div>

        </div>
    </div>
    <input type="submit"  value="Edit Product">
</form>

Please what am I doing wrong?

Thanks for taking the time to review my code

CodePudding user response:

Some refinements to add:

  1. You have product from route model binding at first no need to find it again here: $product = Product::find($product->id);

  2. You are passing the products but your route needs product.

  3. Instead of {{ method_field('PUT') }} use @method('PUT') in your form.

  4. If you had validation you had the data after validation and this code

    $product->update([ 'name' => $request->name, 'category' => $request->category, 'price' => $request->price, 'description' => $request->description, 'status' => $request -> status, 'estimated_delivery_time' => $request->estimated_delivery_time, 'available_quantity' => $request->available_quantity, 'colors' => $request->colors, 'supplier_name' => $request->supplier_name, 'supplier_phone' => $request->supplier_phone, 'video_description' => $request->video_description, 'primary_image' => $insert_array[0], 'image_1' => $insert_array[1], 'image_2' => $insert_array[2], ]);

    would be transferred to

    $validatedData = $request->validate([ .... ]); $product->update($validatedData [ 'primary_image' => $insert_array[0], 'image_1' => $insert_array[1], 'image_2' => $insert_array[2], ]);

    or even moving the validation to a separate form request class.

  • Related