Laravel 重定向数据不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21644524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:59:45  来源:igfitidea点击:

Laravel redirect with data not working

phplaravelblade

提问by Kuurde

My application is a simple CRUD application. I have a delete controller action which redirects back to the list when the item is successfully deleted. What I'm trying to add now is a message to the user that the data was successfully deleted.

我的应用程序是一个简单的 CRUD 应用程序。我有一个删除控制器操作,当项目成功删除时,它会重定向回列表。我现在要添加的是一条消息,告诉用户数据已成功删除。

My Controller Action:

我的控制器操作:

public function deleteItem($id)
{
    try {
        $item = Item::findOrFail($id);
        $item->delete();
        return Redirect::to('list')->with('message', 'Successfully deleted');
    } catch (ModelNotFoundException $e) {
        return View::make('errors.missing');
    }
}

The part of my list.blade.php view where I try to display the message:

我尝试显示消息的 list.blade.php 视图部分:

@if (isset($message))
    <div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        {{ $message }}
    </div>
@endif

The problem that I have is that the $messagevariable is always empty ..

我遇到的问题是$message变量总是空的..

回答by Kumar V

Since the with method flashes data to the session, you may retrieve the data using the typical Session::getmethod.

由于 with 方法将数据闪存到会话中,因此您可以使用典型Session::get方法检索数据。

So you have to get as

所以你必须得到

$message = Session::get('message');

回答by Raynal Gobel

You can use this on your blade template:

您可以在刀片模板上使用它:

@if( Session::has('message') )
    <div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        {{ Session::get('message') }}
    </div>
@endif

Reference: easylaravelblog

参考: easylaravelblog

回答by Rajesh

You can check APP/kernel.php and look for this:

您可以检查 APP/kernel.php 并查找:

\Illuminate\Session\Middleware\StartSession::class,

Check if it is defined more than once like in this example:

检查它是否像本例中一样被多次定义:

protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
];

protected $middlewareGroups = [
'web' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

You can comment any one of the two or delete it. We need to define it one time only.

您可以评论两者中的任何一个或将其删除。我们只需要定义一次。