Laravel - 重定向数据和消息

时间:2017-07-26 19:54:42

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

这是我的分类列表页面。

enter image description here

要加载此页面,控制器代码为

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页面中,我将会话名称删除以显示消息。 但每当我点击删除按钮时,数据都会被删除,但它会显示如下。 enter image description here

欢迎任何帮助或建议。

根据要求,我的查看文件代码如下。谢谢。

    @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

5 个答案:

答案 0 :(得分:1)

试试这个: -

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

答案 1 :(得分:1)

尝试以下代码。

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

答案 2 :(得分:0)

你可以试试这个

class DashboardTemplateView(TemplateView):
    template_name = "base.html"
    context_object_name = 'name'

    def get_context_data(self, *args, **kwargs):
        context = super(DashboardTemplateView,self).get_context_data(*args, **kwargs)
        context["title"] = "This is about us"
        return context

class MyView(ContextMixin, TemplateResponseMixin, View):
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        # mission_statement = Content.objects.filter(Content.objects.title == 'test')
        # context = {'mission_statement' : mission_statement,
        #           'content_list' : Content.objects.all(),
        #           'post_list' : Post.objects.all()}

        # context = {'content_list' : Content.objects.all(), 'post_list' : Post.objects.all()}
        home_list = list(Post.objects).order_by('-id')[0]
        context = {'content_list' : Content.objects.all(), 'home_list' : home_list.objects.all()}
        return self.render_to_response(context)

答案 3 :(得分:0)

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

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

中查看会话文档

答案 4 :(得分:0)

在您的控制器中编写这些代码

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;

我希望这对你有用