我们试图在 laravel 的刀片模板中获取使用重定向方法发送的值。
以下是我的return
语句的样子:
public function movetotrash($id){
$page = Pages::where('id', $id) -> first();
$page -> active = 0;
//$page -> save();
//return redirect()->route('pages.index')->with('trash','Page Moved To Trash');
return redirect('pages')->with('trash','Page Moved To Trash');
}
现在,一旦它重定向到它创建的pages
就是像这样 http://localhost:8888/laravelCRM/public/pages 的URL。
然后我想用$trash变量向用户打印"页面已移至废纸篓"的消息。为此我使用了这个:
@if(isset($trash) ))
<div class="alert alert-error">
{{ $trash }}<br><br>
</div>
@endif
我应该怎么做才能打印值?
谢谢!(提前)
with 方法将数据闪烁到会话,该会话只能在下一个请求中访问。因此,您可以像这样从会话中检索它:
@if(session()->has('trash'))
<div class="alert alert-error">
{{ session()->get('trash') }}
</div>
@endif
在重定向中,值作为会话闪存传递,所以你需要像这样检查值
@if(session()->has('trash'))
然后你可以像这样显示
{{ session('trash') }}
你可以像这样从控制器发送数据,
您必须将消息分配到变量中
$trash = Page Moved To Trash';
return redirect('pages')->with('trash');
然后在视野中,像这样使用。
@if(session($trash) ))
<div class="alert alert-error">
{{ session($trash) }}<br><br>
</div>
@endif
数据将存储在会话中,我们可以在重定向页面上使用它
希望这对:)有所帮助