0

Okay, Here is the store method in my controller:

public function store(EmployeeRequest $request)
{
    if($request->hasFile('photo')){
        $path = $request->photo->store('employeeimages');
// This should have changed the element in the $request['photo'] index:
        $request->photo = $path; 
    dd($request->all());
    }
    Employee::create($request->all());
    return redirect('dashboard');
}

I thought $request->photo gives us the access to the $request['photo'] element of the array, so I tried to update it by $request->photo = $path; but when I die and dump $request->all(), $request['photo'] is not updated and still holds a reference to the previous value, which is an instance of the UploadedFile class. How do I change an element of the $request array?

1 Answer 1

1

Storing the inputs to a new variable and then modifying it solves the issue. This is what I have done:

public function store(EmployeeRequest $request)
{
    if($request->hasFile('photo')){
        $input = $request->all();
        $path = $request->photo->store('employeeimages');
        $input['photo'] = $path;
    }
    Employee::create($input);
    return redirect('dashboard');
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can use $request->merge($array) as well.

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.