Klaas Weerstand
Klaas Weerstand

Reputation: 77

How to fix 'The POST method is not supported for this route. Supported methods: GET, HEAD.'?

I'm trying to make a website where you can upload games and see them on the home page, but because I need a two-step form I can't directly send them to the database.

my controller:

public function createstep1(Request $request)
{
    $naam = $request->session()->get('naam');
    return view('games.game',compact('naam', $naam))->with('games',          game::all());
}

public function postcreatestep1(Request $request)
{
    $validatedData = $request->validate([
        'naam' => 'required',
        'geslacht' => 'required',
    ]);

    if(empty($request->session()->get('naam'))){
    return redirect('/game');
    }else{
        $naam = $request->session()->get('naam');
        $naam->fill($validatedData);
        $request->session()->put('naam', $naam);
    }
    return redirect('/newgame');
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create(Request $request)
{
    $naam = $request->session()->get('naam');
    return view('games.newgame',compact('naam',$naam));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $game= new Game();
    $game->game= $request['game'];
    $game->naam= $request['naam'];
    $game->geslacht= $request['geslacht'];
    $game->save();

    return redirect('/game');
}





<div id="newgame" style="height: 500px; width: 250px; float: left;">
<form method="post" name="naam">
    @csrf
<input id="naam" name="naam" placeholder="naam">
    <select name="geslacht" type="text">
        <option name="man">man</option>
        <option name="vrouw">vrouw</option>
    </select>
<a type="submit"><button>volgende</button></a>
</form>
</div>
<div id="games" style="float: right">
@foreach($games as $game)
    {{$game->game}} <br><br>
@endforeach
</div>




<h1>welkom {{$naam}}</h1>
<form method="post">
    @csrf
    <h3>game invoeren</h3>
    <input type="text" id="gamenaam" name="gamenaam" placeholder="game">
<a id="submit" type="post" name="game" href="/newgame/store">     
<button>verstuur</button></a>
</form>



Route::get('/', function () {
    return view('welcome');
});

Route::get('/newgame', function () {
    return view('games.newgame');
});

//Route::post('/newgames', ['as' => '/newgames', 'uses' => 'GameController@create']);

Route::get('/game', 'GameController@createstep1');
Route::post('/game', 'GameController@postcreatestep1');

Route::get('/newgame', 'GameController@create');
Route::post('/newgame/store', 'GameController@store');

I expect it will drop the game + name and gender in the database but the actual code gives an error message. This work is for school but they can't help me if you have suggestions or can help me fix the problem that would be great.

Upvotes: 0

Views: 866

Answers (1)

Dan
Dan

Reputation: 5358

When registering a new route you need to specify which HTTP method the route is for. Usually Route::get() is alright but since you're sending a POST request with your form to the controller method postcreatestep1, this one needs to be registered as a POST route with Route::post().

You may also declare a route available for multiple HTTP methods with the Route::match() method.

To get an overview of all available router methods the official documentation of Laravel is a good starting point.

Upvotes: 1

Related Questions