Hiroki
Hiroki

Reputation: 4173

Laravel 5 - withInput not working when used with redirect

What I have been trying to do is to send a parameter as an input value and redirect with Laravel 5.2.

I have referred to the official documentaion, some postings on Stackoverflow and Laracast, and here are my attempts.

//This controller function is run by a POST method and gets an input value
public function makeCustomerId()
{
    return redirect('next')
        ->withInput();
}

Below is my next attempt, based on the documentation.

(The input value can be obtained by $this->request->input('id', ''))

public function makeCustomerId()
{
    return redirect()
         ->action('PageController@showNextPage', ['inputId' => $this->request->input('id', '')]);
}

I used the dd() function for debugging controllers, but nothing was sent for both cases.

Do you see anything I'm doing wrong? Any advice will be appreciated!

EDIT

I add some more information.

The controller function above is run from the following form. This will eventually lead to PageController@showNextPage.

    <form action="{{url('makeCustomerId')}}" method="POST">
            <select name="id">
                @foreach ($teams as $team)
                <option value="{{$team->id}}">{{$team->id}}</option>
                @endforeach
            </select>
            <br>
            <input type="submit" value="Submit">
    </form>

Maybe it's unclear, but the input parameter ($this->request->input('id', '')) corresponds to $team->id.

Below is how I use $this->request. In a nutshell, this is dependency injection, meaning that I use the request class from anywhere in the controller class by $this->request

use Illuminate\Http\Request;

class PageController extends Controller {
protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

}

Upvotes: 3

Views: 1165

Answers (1)

tam5
tam5

Reputation: 3237

When you call:

return redirect()
     ->action('PageController@showNextPage', ['inputId' => $this->request->input('id', '')]);

what you are essentially doing is making a new request with the 'inputId' as input. So in your showNextPage method you would also need to access the 'inputId' by saying something like this:

public function showNextPage()
{
    $id = $this->request->input('inputId');

    // do stuff
    // return next page
}

Upvotes: 1

Related Questions