1

I have a form that submits to a controller, which validates the data. If the validation fails it redirects back with the input and the errors. This is the method that deals with the form submission:

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller {

    /**
     * Create a new user.
     *
     * @param Reqeust       $request
     *
     * @return Void
     */
    public function postCreate(Request $request)
    {
        $user = new User;
        $rules = $user->rules();
        $rules['password'] = 'required|confirmed|min:8';
        $v = \Validator::make($request->except('_token', 'roles'), $rules);
        if ($v->fails())
        {
            return redirect()->back()->withInput($request->except('_token', 'password', 'password_confirmation'))->withErrors($v);
        }
        $user->fill($request->except('_token', 'password', 'password_confirmation'));
        $user->password = \Hash::make($request->input('password'));
        $user->save();

        return redirect()->route('webmanAccounts')->with('messages', [['text' => 'User account created', 'class' => 'alert-success']]);
    }

On the page that displays the form I check to see if name, one of the fields, is present and if so populate a User object with the data. The problem is input is always empty.

<?php namespace BackEnd;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Request as RequestFacade;
use App\Http\Controllers\Controller;
use App\Models\Role;
use App\Models\User;

class UserController extends Controller {

    public function __construct(Request $request)
    {
        if ( ! $request->user()->can('accounts'))
        {
            return abort(403, 'You do not have permission to access this page.'); 
        }
    }

    /**
     * Display the create new user form and process any error messages.
     *
     * @param Reqeust       $request
     *
     * @return View
     */
    public function create(Request $request)
    {
        $user = new User;
        dump(RequestFacade::all());
        if (RequestFacade::has('name'))
        {
            $user->fill(RequestFacade::except('_token', 'roles'));
            foreach (RequestFacade::only('roles') as $role)
            {
                $user->roles()->add($role);
            }
        }
        return view('backend.user.create', ['title' => 'Website Manager :: Create New Account', 'user' => $user, 'roles' => Role::all()]);
    }

I have tried RequestFacade, $request and Input, all show as empty. Why isn't the data being passed back?

To add to the strangeness of this, I have another project that uses almost identical code and that works perfectly fine. Why would it work fine for one project but not for another!?

2
  • The moment you physically redirect a user you lose all post data. Store the post data in a session before redirecting. Or render the post form on the error finding page. Commented Jul 2, 2015 at 7:41
  • @MichaelDibbets That's not true. As I said in my post, using withInput() works in my other project perfectly fine. Commented Jul 2, 2015 at 7:50

1 Answer 1

7

When you use the withInput() method, the data is flashed to the session as "old" data.

$request->old() should give you an array of all the "old" data.

$request->old('name') should give you the "old" name data.

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

3 Comments

That works, thank you, but why does Request::input() work on my other project but not this one? Both are Laravel 5.1, the code is near identical. I don't get it.
@Styphon Request::input() gets the input from the $_GET and $_POST superglobals. The only way your other project would work is if the data is coming through the get/post request data, and not from a redirect()->back()->withInput() situation (which transfers the data by flashing it to the session).
<input type="text" name="line_1" value="<?php echo $request->old('line_1', $address->line_1); ?>"> - the address object being the default

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.