Home > Back-end >  Laravel form submit unsuccessful returns blank page instead of redirect home
Laravel form submit unsuccessful returns blank page instead of redirect home

Time:10-20

I have a laravel form to create a new product entry in a database on submit, am supposed to be redirected back but I get a completely blank page without any errors. There is no new entry in my database when I check. The form is made up of various text fields, an image URL, and a multi-selected Image URL

please this is my blade template

<form method="POST" action="{{ route('products.store') }}" enctype="multipart/form-data">
                                <h4 >Create Product</h4><br><br>
                                @csrf

                                <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="name"  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="catchy_title"  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="long_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('upload/products/' . $product->image) : url('upload/no_image.jpg') }}"
                                                alt="Hero image">
                                        </div>
                                    </div>
                                    <!-- end row -->
                                </div>
                                <div >
                                    <div >
                                        <label for="example-text-input" >Add Other
                                            Images</label>
                                        <div >
                                            <input name="multi_image[]" accept="image/*" 
                                                type="file" id="image" multiple="">
                                        </div>
                                    </div>
                                    <!-- end row -->
                                </div>
                            </div>
                            <input type="submit" 
                                value="Create Product">
                            </form>

and this is my store function

 public function store(Request $request)
{
    if ($request->file('image')) {
        $image = $request->file('image');
        $name_gen = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();  // 3434343443.jpg

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

        $this->validate($request, [
            'name' => $request->name,
            'category' => $request->category,
            'price' => $request->price,
            'description' => $request->description,
            'status' => $request -> status,
            'tags' => $request -> tags,
            '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' => $save_url,
            'other_images' => $save_url,

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

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

Please what am I doing wrong?

thank you for taking some time to review

CodePudding user response:

you are validating the request but not saving it to database. after the validation and before the return you should do like this to save in database.

            $item=new YOUR_MODEL_NAME:
            $item->name => $request->name,
            $item->category => $request->category,
            $item->price => $request->price,
            $item->description => $request->description,
            $item->status => $request -> status,
            $item->tags => $request -> tags,
            $item->estimated_delivery_time => $request->estimated_delivery_time,
            $item->available_quantity => $request->available_quantity,
            $item->colors => $request->colors,
            $item->supplier_name => $request->supplier_name,
            $item->supplier_phone => $request->supplier_phone,
            $item->video_description => $request->video_description,
            $item->primary_image => $save_url,
            $item->other_images => $save_url,
            $item->save();

CodePudding user response:

Try to be specific, pass route where you want to redirect like below:

return redirect()->route('route-name-here')->with($notification);
  • Related