Laravel 重定向返回 with() 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19838978/
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-09 02:39:57  来源:igfitidea点击:

Laravel Redirect Back with() Message

laravellaravel-5laravel-4

提问by Mudit Tuli

I am trying to redirect to the previous page with a message when there is a fatal error.

当出现致命错误时,我试图通过一条消息重定向到上一页。

App::fatal(function($exception)
{
    return Redirect::back()->with('msg', 'The Message');
}

In the view trying to access the msg with

在试图访问 msg 的视图中

Sessions::get('msg')

But nothing is getting rendered, am I doing something wrong here?

但是没有任何东西被渲染,我在这里做错了吗?

回答by giannis christofakis

Try

尝试

return Redirect::back()->withErrors(['msg', 'The Message']);

and inside your view call this

在你的视图中调用这个

@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif

回答by Ketan Akbari

Laravel 5

Laravel 5

Controller

控制器

 return redirect()->back()->with('success', 'your message,here');   

Blade:

刀刃:

@if (\Session::has('success'))
    <div class="alert alert-success">
        <ul>
            <li>{!! \Session::get('success') !!}</li>
        </ul>
    </div>
@endif

回答by Rick

Alternative approach would be

替代方法是

Controller

控制器

Session::flash('message', "Special message goes here");
return Redirect::back();

View

看法

@if (Session::has('message'))
   <div class="alert alert-info">{{ Session::get('message') }}</div>
@endif

回答by haakym

In Laravel 5.4 the following worked for me:

在 Laravel 5.4 中,以下对我有用:

return back()->withErrors(['field_name' => ['Your custom message here.']]);

回答by bumerang

You have an error (misspelling):

您有一个错误(拼写错误):

Sessions::get('msg')// an extra 's' on end

Should be:

应该:

Session::get('msg')

I think, now it should work, it does for me.

我想,现在它应该起作用了,它对我有用。

回答by Majbah Habib

Just set the flash message and redirect to back from your controller functiion.

只需设置 flash 消息并从您的控制器功能重定向回来。

    session()->flash('msg', 'Successfully done the operation.');
    return redirect()->back();

And then you can get the message in the view blade file.

然后您可以在查看刀片文件中获取消息。

   {!! Session::has('msg') ? Session::get("msg") : '' !!}

回答by Sergio

In Laravel 5.5:

在 Laravel 5.5 中

return back()->withErrors($arrayWithErrors);

In the view using Blade:

在使用 Blade 的视图中:

@if($errors->has())
    <ul>
    @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
    </ul>
@endif

回答by justijn

In laravel 5.8 you can do the following:

在 Laravel 5.8 中,您可以执行以下操作:

return redirect()->back()->withErrors(['name' => 'The name is required']);

and in blade:

并在刀片中:

@error('name')
<p>{{ $message }}</p>
@enderror

回答by Hatim Hussein

in controller

在控制器中

For example

例如

return redirect('login')->with('message',$message);

in blade fileThe message will store in session not in variable.

在刀片文件中,消息将存储在会话中而不是变量中。

For example

例如

@if(session('message'))
{{ session('message') }}
@endif

回答by DutGRIFF

I stopped writing this myself for laravel in favor of the Laracasts packagethat handles it all for you. It is really easy to use and keeps your code clean. There is even a laracastthat covers how to use it. All you have to do:

我不再自己为 laravel 写这个了,转而使用Laracasts 包来为你处理这一切。它非常易于使用并保持您的代码干净。甚至还有一个laracast介绍了如何使用它。所有你需要做的:

Pull in the package through Composer.

通过 Composer 拉入包。

"require": {
  "laracasts/flash": "~1.0"
}

Include the service provider within app/config/app.php.

在 app/config/app.php 中包含服务提供者。

'providers' => [
  'Laracasts\Flash\FlashServiceProvider'
];

Add a facade alias to this same file at the bottom:

在底部为同一个文件添加一个外观别名:

'aliases' => [
  'Flash' => 'Laracasts\Flash\Flash'
];

Pull the HTML into the view:

将 HTML 拉入视图:

@include('flash::message') 

There is a close button on the right of the message. This relies on jQuery so make sure that is added before your bootstrap.

消息右侧有一个关闭按钮。这依赖于 jQuery,因此请确保在引导之前添加。

optional changes:

可选更改:

If you aren't using bootstrap or want to skip the include of the flash message and write the code yourself:

如果您不使用引导程序或想跳过 Flash 消息的包含并自己编写代码:

@if (Session::has('flash_notification.message'))
  <div class="{{ Session::get('flash_notification.level') }}">
    {{ Session::get('flash_notification.message') }}
  </div>
@endif

If you would like to view the HTML pulled in by @include('flash::message'), you can find it in vendor/laracasts/flash/src/views/message.blade.php.

如果您想查看由 拉入的 HTML @include('flash::message'),您可以在vendor/laracasts/flash/src/views/message.blade.php.

If you need to modify the partials do:

如果您需要修改部分,请执行以下操作:

php artisan view:publish laracasts/flash

The two package views will now be located in the `app/views/packages/laracasts/flash/' directory.

这两个包视图现在将位于“app/views/packages/laracasts/flash/”目录中。