我正在用Laravel开发带有联系表格的软件包。我正在尝试设置和获取会话数据,但不适用于以下内容。
查看
GetFiles
控制器
@if(session('success'))
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
{{session('success')}}
</div>
@endif
路线
public function store(Request $request)
{
//Validation
$request->validate([
'email'=>'required|max:50|unique:contact_forms,email'
]);
//Data
$contact_form = new ContactForm();
$contact_form->full_name = $request->full_name;
$contact_form->mobile = $request->mobile;
$contact_form->email = $request->email;
//Save
$contact_form->save();
//Return back
return back()->with('success','Record inserted successfully');
}
如何在Laravel的软件包开发中设置/获取会话数据?谢谢。
答案 0 :(得分:1)
执行此操作的最佳方法是Flash会话消息
重定向为
$request->session()->flash('alert-success', 'Record inserted successfully!');
并在您的查看文件中
<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">×</a></p>
@endif
@endforeach