获取同一控制器重定向的值,以在 Laravel 中使用消息进行路由


Get the value of same controller redirect to route with a Message in Laravel

我是 Laravel 的新手,我正在尝试获取由函数名称发送的参数的值movetotrash()这是我的函数的样子:

public function movetotrash($id){
        $page = Pages::where('id', $id) -> first();
        $page -> active = 0;
        $page -> save();
        return redirect()->route('pages.index')->with('trash','Pages Moved To Trash');
    }

在同一个控制器中,我有这个功能:

public function index()
    {

        $pages=Pages::where('active', 1)->get();
        //get the value of "trash" sent by function movetotrash()
        return view('pages.pages')->with('pages',$pages);
    }

我该怎么做?然后,我将此消息从前端发送到前视图,以便它可以通知用户该值已成功移至垃圾箱。

有没有简单的方法或其他方法可以做到这一点?如果是,那么也建议这样做。

谢谢!

在您的

视图中添加此代码,

   <?php $mess = Session::get('trash'); ?>
    @if (isset($mess ))
       <div class="alert alert-error">
          {{ Session::get('trash') }}<br><br>
       </div>
    @endif