Laravel 返回视图显示消息

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

Laravel return view show message

phplaravelviewlaravel-5.1message

提问by utdev

How can I show a message after I return a view in my Controller.

在我的控制器中返回视图后如何显示消息。

I am using Laravel 5.1.

我正在使用 Laravel 5.1。

 return view('pr.new', [
      'errorMessageDuration' => 'error too long',
      'route' => 'createPr',
      'type' => 'new',
 ]);

I tried to call the message like this:

我试图这样调用消息:

  @if(session('errorMessageDuration'))
         <div class="alert alert-danger">
             <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
             {{ session('errorMessageDuration') }}
             {{ Input::get('title') }}
         </div>
  @endif

But it did not work, any ideas?

但它没有用,有什么想法吗?

回答by Rakesh Kumar

Use code Like This

像这样使用代码

@if(isset($errorMessageDuration))
         <div class="alert alert-danger">
             <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
             {{ $errorMessageDuration }}
             {{ Input::get('title') }}
         </div>
  @endif

回答by geckob

If you are using Redirect, then you need to use session like what you did,

如果你正在使用Redirect,那么你需要像你所做的那样使用 session,

Controller:

控制器

return redirect('dashboard')->with('errorMessageDuration', 'Error!');

View:

查看

@if(empty(session('errorMessageDuration')))
         <div class="alert alert-danger">
             <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
             {{ session('errorMessageDuration') }}
             {{ Input::get('title') }}
         </div>
@endif

But if you pass the variables using Viewfacade, then you should just like what Rakesh showed:

但是如果你使用ViewFacade传递变量,那么你应该就像 Rakesh 展示的那样:

Controller:

控制器

 return view('pr.new', [
      'errorMessageDuration' => 'error too long',
      'route' => 'createPr',
      'type' => 'new',
 ]);

View:

查看

@if(isset($errorMessageDuration))
         <div class="alert alert-danger">
             <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
             {{ $errorMessageDuration }}
             {{ Input::get('title') }}
         </div>
@endif