3
votes

There is no problem adding data to the database but after adding the data to the database, redirect is not working. It keeps me on the "saving" page

Here is my controller;

public function postSave(Request $gamedata)
{
    $postcheck = Validator:: make($gamedata -> all(), array(

        'game_name' => 'required|min:3',
        'game_year' => 'numeric',
        'game_type' => 'required',
    ));
   if ($postcheck-> fails()) {
       return redirect() -> to('/')->withErrors($postcheck)->withInput();
   } else {

       $game_name = $gamedata -> input('game_name');
       $game_year = $gamedata -> input('game_year');
       $game_type = $gamedata -> input('game_type');

       $saving= games::create(array('name' =>$game_name , 'year' =>$game_year , 'type' =>$game_type));
   }
if ($saving){
    redirect() -> route('index') ;
}
return null;
}

my form;

<div class="col-md-6">
    <form action="{{url('/saving')}}" method="post">
        {{csrf_field()}}
        <div class="form-group">
            <label for="game_name">Game Name</label>
            <input type="text" class="form-control" name="game_name" placeholder="Game Name">
        </div>
        <div class="form-group">
            <label for="exampleInputPassword1">Release Date</label>
            <input type="number" class="form-control" name="game_year" placeholder="Release Date">
        </div>
        <div class="form-group">
            <label for="Game_Type">Game Type</label>
            <input type="text" class="form-control" name="game_type" placeholder="Game type">
        </div>
        <button type="submit" class="btn btn-default" name="gameinfo_save">Add a new game</button>

    </form>
</div>

and routes;

    Route::get('/', array ('as' => 'index', 'uses' =>'homecontroller@getIndex')); 
    Route::post('/saving', array ('as' => 'saving', 'uses' =>'homecontroller@postSave'));

This not a project, I'm just trying to learn.. So where do i mistake ?

3

3 Answers

8
votes

used this for redirect in laravel and your view name is must be index.blade.php

return redirect('index');

and also you can redirect like this

return redirect()->route('index'); 
1
votes

Have a look here, from your code: redirect() -> route('index') ;

you need to get rid of the spaces as well! return redirect()->route('index') ;

In this situation the spaces are important, they affect how the code is executed: [ 'foo' => 'bar' ] (spaces don't matter)

$company->employeeOfTheMonth (valid, works)

$company -> employeeOfTheMonth (invalid, doesn't work)

I'm guessing you're asking that question because Laravel doesn't throw an error - but that doesn't mean it does what you expect (because of the space).

0
votes

In your Index.blade.php use this...

return redirect()->route('index')->with('success','Write here your messege');