简体   繁体   English

Laravel 5.4重定向不起作用

[英]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 ? 这不是一个项目,我只是想学习。

used this for redirect in laravel and your view name is must be index.blade.php 用于在laravel中重定向,您的视图名称必须是index.blade.php

return redirect('index');

and also you can redirect like this 而且您也可以像这样重定向

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

Have a look here, from your code: redirect() -> route('index') ; 在您的代码redirect() -> route('index') ;一下: 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) 在这种情况下,空格很重要,它们会影响代码的执行方式: [ 'foo' => 'bar' ] (空格无关紧要)

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

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

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). 我猜您是在问这个问题,因为Laravel不会抛出错误-但这并不意味着它可以完成您所期望的(由于空间)。

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

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


您还可以使用控制器或类文件来重新定义页面和数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM