Laravel 5.4 Redirect无法正常工作

时间:2017-07-07 12:10:30

标签: php laravel-5.4

向数据库添加数据没有问题,但在将数据添加到数据库后,重定向无效。它使我保持"拯救"页

这是我的控制器;

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;
}

我的表格;

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

和路线;

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

这不是一个项目,我只是想学习..那我在哪里弄错?

4 个答案:

答案 0 :(得分:5)

将其用于laravel中的重定向,您的视图名称必须为index.blade.php

return redirect('index');

也可以像这样重定向

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

答案 1 :(得分:1)

从您的代码中查看: redirect() -> route('index') ;

你也需要摆脱这些空间! return redirect()->route('index') ;

在这种情况下,空格很重要,它们会影响代码的执行方式: [ 'foo' => 'bar' ](空间不重要)

$company->employeeOfTheMonth(有效,有效)

$company -> employeeOfTheMonth(无效,无效)

我猜你是在问这个问题,因为Laravel并没有抛出错误 - 但这并不意味着它符合你的期望(因为空间)。

答案 2 :(得分:0)

在Index.blade.php中使用此...

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


答案 3 :(得分:0)

  

您还可以使用控制器或类文件来重新显示页面   以及数据。

return redirect('home/managers')->with('success','Manager add successfully !');
相关问题