如何在Laravel 5.1中显示保存成功消息?

时间:2015-06-21 05:43:25

标签: php laravel

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

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

7 个答案:

答案 0 :(得分:77)

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

$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");
}

Laravel 5.4关于Flash数据: 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

答案 1 :(得分:15)

使用代码

return redirect()->route("photo.index")->with('message','Success');

并在你的模板中

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

答案 2 :(得分:12)

非常简单

client_connection.sendall(ip+":"+port)
try:
    client_connection.shutdown(socket.SHUT_WR)  # signal end of strem
    while True:  # wait for the client to close or shutdown his side
        q = client_connection.recv(1024)
        if len(q) == 0:
            break
finally:
    client_connection.close()  # close the socket only after the client has close its side

在主视图或索引中 加上这个。

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();

    //PUT HERE AFTER YOU SAVE
    \Session::flash('flash_message','successfully saved.');

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

如果您想要使用不同的闪光灯样式,请查看Flash Messages in Laravel 5

答案 3 :(得分:7)

您可以重定向用户并向他显示Flash消息。该邮件将以这种方式显示在{{ $message }}

return redirect('user/login')->with('message', 'Success!');

有关此内容的更多信息,您可以找到here

答案 4 :(得分:6)

使用关闭按钮显示成功消息:

控制器代码:

$seeder->save();    
return redirect('photo.index')->with('status', 'You have successfully Created!');

只需将以下代码粘贴到您的视图文件中即可。

@if (session('status'))
        <div class="alert alert-success">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a> {{ session('status') }}
        </div>
    @endif

答案 5 :(得分:3)

在Laravel 5.5中,你可以使用......

在您的控制器操作中:

try {
    Get-ADUser -Filter {samAccountName -eq "SomeUser"} -SearchBase "DC=other_city,DC=example,DC=com" -Server example.com
} Catch {
    $_.Exception.Referral
}

然后在您的模板中,您可以使用会话助手:

...
return redirect('photo.index')->with('alert-success', 'The data was saved successfully');

答案 6 :(得分:2)

最好的方式 - &gt; https://github.com/laracasts/flash

只需闪屏(&#39;留言&#39;,&#39;信息&#39;)