如何在Laravel 5.1中显示保存成功消息? [英] How to show Save success message in Laravel 5.1?

查看:105
本文介绍了如何在Laravel 5.1中显示保存成功消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5.1中,我可以将数据保存在数据库中.但我想展示一个成功的信息.我该怎么做?我在Controller保存代码中的代码是

In Laravel 5.1, I can save the data in the database. But I would like to show a success message. How can I do it? My code in Controller save code is

public function store(Request $request)
{

    $this->validate($request,[
        'name'=>'required|unique:seeders|max:255',
        'address'=>'required`enter code here`',
        'age'=>'required',
    ]);

    $seeder=new Seeders();
    $seeder->name=$request->input('name');
    $seeder->address=$request->input('address');
    $seeder->age=$request->input('age');
    $seeder->save();

    return redirect()->route("photo.index");
} // save data

推荐答案

只需在重定向代码之前添加以下代码:

Just adding this code before your redirect code:

$request->session()->flash('alert-success', 'User was successful added!');

因此完整的代码如下:

public function store(Request $request)
{
    // your function

    $request->session()->flash('alert-success', 'User was successful added!');
    return redirect()->route("photo.index");
}

关于Flash数据的Laravel 5.4: https://laravel.com/docs/5.4/session#flash-data

Laravel 5.4 about Flash Data: https://laravel.com/docs/5.4/session#flash-data

并提出您的看法:

<div class="flash-message">
    @foreach (['danger', 'warning', 'success', 'info'] as $msg)
      @if(Session::has('alert-' . $msg))

      <p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></p>
      @endif
    @endforeach
  </div> <!-- end .flash-message -->

您可以使用Bootstrap Alerts视图: http://www.w3schools.com/bootstrap/bootstrap_alerts .asp

You can use Bootstrap Alerts view: http://www.w3schools.com/bootstrap/bootstrap_alerts.asp

这篇关于如何在Laravel 5.1中显示保存成功消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆