user7986752
user7986752

Reputation:

Laravel 5.4 Redirect not working

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 ?

Upvotes: 3

Views: 19007

Answers (3)

Rocky
Rocky

Reputation: 483

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

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


Upvotes: 0

lehel
lehel

Reputation: 451

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).

Upvotes: 1

Bilal Ahmed
Bilal Ahmed

Reputation: 4066

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'); 

Upvotes: 8

Related Questions