Laravel 5.4 以不同方式显示 flash 错误和成功消息

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

Laravel 5.4 Show flash error and success messages in different ways

laravel

提问by Heidel

Method from my PostController

来自我的 PostController 的方法

public function store(PostRequest $request)
    {
        if (Post::create($request->all())) {
            $request->session()->flash('status', 'Post was successfully added!');
        } else {
            $request->session()->flash('status', 'Error!');
        }
        return redirect('/');
    }

index view

索引视图

<?php if(session()->has('status')){
        echo '<div style="text-align: center">';
        echo session()->get('status');
        echo '</div>';
    }?>

How to show error and success messages in different ways?

如何以不同的方式显示错误和成功消息?

回答by Paras

You can use something like this:

你可以使用这样的东西:

public function store(PostRequest $request) {
    if (Post::create($request->all())) {
        $request->session()->flash('message.level', 'success');
        $request->session()->flash('message.content', 'Post was successfully added!');
    } else {
        $request->session()->flash('message.level', 'danger');
        $request->session()->flash('message.content', 'Error!');
    }
    return redirect('/');
}

In your blade file:

在您的刀片文件中:

@if(session()->has('message.level'))
    <div class="alert alert-{{ session('message.level') }}"> 
    {!! session('message.content') !!}
    </div>
@endif

The code above uses bootstrap's alert css classesfor styling and is inspired by Jeffrey Way's Laracasts Flash package

上面的代码使用bootstrap 的 alert css 类进行样式设置,灵感来自Jeffrey Way 的 Laracasts Flash 包

回答by Muhammad Shahzad

You can display flash messages in this way:

您可以通过以下方式显示 Flash 消息:

In your controller:

在您的控制器中:

$request->session()->flash('success', 'Record successfully added!');
//OR
$request->session()->flash('warning', 'Record not added!');

In your veiw:

在你看来:

@foreach (['danger', 'warning', 'success', 'info'] as $key)
 @if(Session::has($key))
     <p class="alert alert-{{ $key }}">{{ Session::get($key) }}</p>
 @endif
@endforeach

回答by Madhav Kankraan

  1. Write code like this in your Blade template:

    @if(Session::has('error'))
        <p class="alert alert-danger">{{ Session::get('error') }}</p>
    @endif
    
  2. Write code like this in your Controller:

    Session::flash('error', 'Some thing is wrong. Please try again');
    Redirect::to(path to blade file route);
    
  1. 在您的 Blade 模板中编写如下代码:

    @if(Session::has('error'))
        <p class="alert alert-danger">{{ Session::get('error') }}</p>
    @endif
    
  2. 在您的控制器中编写这样的代码:

    Session::flash('error', 'Some thing is wrong. Please try again');
    Redirect::to(path to blade file route);
    

回答by Mateusz

You can use ->with()in your controller

您可以->with()在控制器中使用

public function store(PostRequest $request) {
    if (!Post::create($request->all())) {
        return redirect('/')->with('error', 'Error!');
    }

    return redirect('/')->with('success', 'Post was successfully added!');
}

Your view could look like this:

您的视图可能如下所示:

@if (session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif
@if (session('error'))
    <div class="alert alert-danger">
        {{ session('error') }}
    </div>
@endif

or (with additional CSS for alert-errorclass):

或(带有额外的alert-error类CSS ):

@foreach (['error', 'success'] as $status)
    @if(Session::has($status))
        <p class="alert alert-{{$status}}">{{ Session::get($status) }}</p>
    @endif
@endforeach