64

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";. But then, I get the error message saying Can't use function return value in write context.

Is it possible for us change the input value so that when later calling on Input::get('inputname') will get the new amended value?

Thanks.

1
  • You must assign it to a variable and then you can perform operations on the variable. The function get of the single pattern Input accepts a string argument and then perform's internal operations on the HTTP request to bring the data back to you, which is why you cannot treat it as a string. However, if you assign the value of that to a variable, then the variable can be manipulated thussly. Commented Apr 15, 2014 at 2:27

7 Answers 7

150

You can use Input::merge() to replace single items.

Input::merge(['inputname' => 'new value']);

Or use Input::replace() to replace the entire input array.

Input::replace(['inputname' => 'new value']);

Here's a link to the documentation

Sign up to request clarification or add additional context in comments.

3 Comments

Note: Input:: is just a facade for app('request'). So $request->merge, ->replace... Work as well. Thx.
For FormRequest also might need to replace inner $request->request (instance of ParameterBag) with $request->request->replace(array_merge($request->request->all(), $newData));
$request->replace instead of Input::replace removed all data from the input except the value I replaced.
11

If you're looking to do this in Laravel 5, you can use the merge() method from the Request class:

class SomeController extends Controller
{
    public function someAction( Request $request ) {

        // Split a bunch of email addresses
        // submitted from a textarea form input
        // into an array, and replace the input email
        // with this array, instead of the original string.
        if ( !empty( $request->input( 'emails' ) ) ) {

            $emails = $request->input( 'emails' );
            $emails = preg_replace( '/\s+/m', ',', $emails );
            $emails = explode( ',', $emails );

            // THIS IS KEY!
            // Replacing the old input string with
            // with an array of emails.
            $request->merge( array( 'emails' => $emails ) );
        }

        // Some default validation rules.
        $rules = array();

        // Create validator object.
        $validator = Validator::make( $request->all(), $rules );

        // Validation rules for each email in the array.
        $validator->each( 'emails', ['required', 'email', 'min: 6', 'max: 254'] );

        if ( $validator->fails() ) {
            return back()->withErrors($validator)->withInput();
        } else {
            // Input validated successfully, proceed further.
        }
    }
}

Comments

10

If you mean you want to overwrite input data, you can try doing:

Input::merge(array('somedata' => 'SomeNewData'));

1 Comment

Except it would be: Input::merge(array('somedata'=> 'SomeNewData'));
9

Try this,it will help you.

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

Comments

2

I also found this problem, I can solve it with the following code:

public function(Request $request)
{
    $request['inputname'] = 'newValue';
}

Regards

Comments

1

I'm using Laravel 8. The following is working for me: $request->attributes->set('name', 'Value');

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.