Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adnanerlansyah403's avatar

How to update part of form request for insert data to table field without change it the input request

Hello friends, can you all help me about my case. So I want to update part from form request to insert data. But there's one problem, which is I must change it the name of the one of form request. So In my form request is there name a "demo1" but actually this name I want to insert to field's "product_discount_percetange". I can't use the other name for "demo1", because this one is what I need for running the scripts from library. So how can I update that part name of "demo1" for insert data to field "product_discount_percentage" without change it the name of input request.

Thanks all 🙏, and sorry friends if my English's bad. I hope you all understand about what's mean of my case.

0 likes
6 replies
adnanerlansyah403's avatar

@MohamedTammam hello dear tammam, thank you for your respons. im glad you're respond my thread. before i explain my problem, this is my codes

Form Request :

<?php

namespace App\Http\Requests\Product;

use Illuminate\Foundation\Http\FormRequest;

class UpdateProductRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'product_name_en' => 'required',
            'product_name_id' => 'required',
            'product_code' => 'required',
            'product_quantity' => 'required|numeric',
            'product_price' => 'required|numeric',
            'product_discount_name' => 'nullable',
            'product_discount_type' =>  'required_with:product_discount_name|nullable',
            'demo1' => 'required_with:product_discount_name|nullable',
            'product_discount_time' => 'required_with:product_discount_name|nullable',
            'product_discount_start_date' => 'required_with:product_discount_name|nullable|date',
            'product_discount_end_date' => 'required_with:product_discount_name|nullable|date',
            'product_tags_en' => 'required',
            'product_tags_id' => 'required',
            'product_size_en' => 'nullable',
            'product_size_id' => 'nullable',
            'product_color_en' => 'nullable',
            'product_color_id' => 'nullable',
            'product_short_description_en' => 'required',
            'product_short_description_id' => 'required',
            'product_long_description_en' => 'required',
            'product_long_description_id' => 'required',
            'hot_deal' => 'nullable',
            'featured' => 'nullable',
            'special_offer' => 'nullable',
            'special_deal' => 'nullable',
            'category_id' => 'required',
            'subcategory_id' => 'required',
            'subsub_category_id' => 'required',
            'brand_id' => 'required',
        ];
    }

    public function messages() 
    {
        return [
            // Required Discount Without Besides Discount Product Name
            'product_discount_name.required_without' => 'Product discount type, percentage, time, start date and end date fields are required when product discount name is filled.',

            // Required Discount With Discount Product Name
            'product_discount_type.required_with' => 'Product discount type field is required when product discount name is filled.',
            'demo1.required_with' => 'Product discount percentage field is required when product discount name is filled.',
            'product_discount_time.required_with' => 'Product discount time field is required when product discount name is filled.',
            'product_discount_start_date.required_with' => 'Product discount start date field is required when product discount name is filled.',
            'product_discount_end_date.required_with' => 'Product discount end date field is required when product discount name is filled.',

        ];
    }

}

Method Update

public function manageUpdateProduct(UpdateProductRequest $request, $id)
    {
        $validated = $request->validated();
        
        $validated['product_slug_en'] = Str::slug($validated['product_name_en']);
        $validated['product_slug_id'] = Str::slug($validated['product_name_id']);

        Product::where('id', $id)->update($validated);

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

        return redirect()->route('manage.product')->with($notification);
    }

So in my case, i want to insert data table to field "product_discount_percentage" with Form Request but there's a problem which is the name's of input request for "product_discount_percentage" is a "demo1". i can't change it that name, because i need it for running the scripts from library. So i must using name "demo1" for insert data to table field "product_discount_percentage". So how can i fix this, without change the name of input request. So it's like i want to overriding the input request "demo1" to be "product_discount_percentage". Can I do that?...

Thank You @mohamedtammam , I hope you understand about what im saying. and sorry if My English's Bad.

undeportedmexican's avatar
Level 15

@adnanerlansyah403

You can add the prepareForValidation() method on your FormRequest, something like this:

protected function prepareForValidation()
{
    $this->merge([
        'product_discount_percetange' => $this->demo_1,
    ]);
}

Then on your validation add the product_discount_percentage rule, and you should be good to go.

1 like

Please or to participate in this conversation.