【问题标题】:Laravel - Redirect with both data and messageLaravel - 使用数据和消息重定向
【发布时间】:2017-07-26 19:54:41
【问题描述】:

这是我的类别列表页面。

要加载这个页面的控制器代码是

public function categorysettings(Request $request)
{
  $categorieslist=productcategory::all();
  return view('categorieslist',compact('categorieslist'));
}

现在当我点击删除按钮时,这段代码就会被执行。

public function deletecategory($categoryid)
{
   $category = productcategory::find($categoryid);
   $category->delete();
   $categorieslist=productcategory::all();
   return Redirect::back()->with('deleted','Successfully Deleted Category !!!');

}

在我的 html 页面中,我删除了会话名称以显示消息。 但是每当我单击删除按钮时,数据就会被删除,但它会像这样显示。

欢迎任何帮助或建议。

根据要求,我的视图文件代码如下。谢谢。

    @if(Session::has('deleted'))
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="alert alert-danger">
                    {{Session::get('deleted')}}
                </div>
            </div>
        </div>
    @endif

【问题讨论】:

  • 数组很酷
  • 您可以在显示deleted 会话消息的位置发布/包含视图文件的代码吗?
  • 添加了视图文件代码。
  • -&gt;with(["deleted" =&gt; "This is your deleted message"]);,或者你可以使用session()-&gt;flash("deleted", "This is your deleted message.");
  • 你使用的是什么版本的 Laravel?

标签: php laravel laravel-5 laravel-5.2 laravel-5.3


【解决方案1】:

在会话中定义已删除的变量

public function deletecategory($categoryid)
{
   $category = productcategory::find($categoryid);
   $category->delete();
   $categorieslist=productcategory::all();
   session()->flash('deleted', 'Successfully Deleted Category !!!');
   return Redirect::back();
}

在laravel网站https://laravel.com/docs/5.4/session#flash-data查看会话文档

【讨论】:

  • 得到同样的输出。
  • 你能做到dd(session()-&gt;all());并把结果粘贴到这里吗?
【解决方案2】:

试试下面的代码。

public function deletecategory($categoryid)
{
   $category = productcategory::find($categoryid);
   $category->delete();
   $categorieslist=productcategory::all();
   return redirect(<Category-list-route>)
       ->with('success', 'Successfully Deleted Category !!!');
}

然后在刀片中

@if(Session::has('success'))
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="alert alert-danger">
                {{Session::get('success')}}
            </div>
        </div>
    </div>
@endif

【讨论】:

    【解决方案3】:

    试试这个:-

    In Your controller
    return redirect()->back()->with('deleted', 'Successfully Deleted Category !!!');
    

    在视图文件中

    @if(Session::has('deleted'))
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="alert alert-danger">
                {!! session('deleted') !!}
            </div>
        </div>
    </div>
    @endif
    

    【讨论】:

      【解决方案4】:

      你可以试试这个

      @if(Session::get('deleted'))
          <div class="row">
              <div class="col-md-8 col-md-offset-2">
                  <div class="alert alert-danger">
                      {{Session::get('deleted')}}
                  </div>
              </div>
          </div>
      @endif
      

      【讨论】:

      • 得到相同的结果。
      【解决方案5】:

      在你的控制器中写下这些代码

      Session::flash('deleted', 'Category information deleted successfully.');
      return Redirect::to('/your-category-list-url'); 
      // Your Listing page which you wrote in web.php under routes folder if you are using laravel 5.4
      // If you are using laravel 5.2 then in your routes.php
      

      现在在您的刀片模板中

      @if(Session::has('deleted'))
      <div class="col-lg-10 pull-right" align="center">
          <div class="alert alert-danger  alert-dismissable " role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>  <i class="fa fa-remove"></i>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  {{ Session::get('deleted') }}
          </div>
      </div>
      @endif
      

      确保在控制器中使用这些

      use Session;
      use Redirect;
      

      我希望这对你有用

      【讨论】:

        猜你喜欢
        • 2018-05-22
        • 2016-07-15
        • 2016-04-06
        • 2020-12-29
        • 2020-05-21
        • 2013-11-19
        • 2016-09-19
        • 2014-04-18
        • 2014-07-06
        相关资源
        最近更新 更多