Laravel change input value

后端 未结 6 1618
灰色年华
灰色年华 2020-12-01 05:45

In laravel, we can get the input value via Input::get(\'inputname\'). I try to change the value by doing this Input::get(\'inputname\') = \"new value\";

6条回答
  •  春和景丽
    2020-12-01 06:35

    I used Raham's answer to solve my problem. However, it was nesting the updated data within an array, when I needed it at the same level as other data. I used:

    $request->merge('someIndex' => "yourValueHere");
    

    A note other Laravel newbies, I used the merge method to account for an empty checkbox value in a Laravel 7 update form. A deselected checkbox on an update form doesn't return 0, it doesn't set any value in the update request. As a result that value is unchanged in the database. You have to check for a set value and merge a new value if nothing exists. Hope that helps someone.

    Just a quick update. If the user doesn't check a box and I need to enter a value in the DB I do something like this in my controller:

        if(empty($request->input('checkbox_value'))) {
            $request->merge(['checkbox_value' => 0]);
        }
    

提交回复
热议问题