Nivas
Nivas

Reputation: 358

Laravel - Redirect with both data and message

This is my categories list page.

enter image description here

To load this page Controller code is

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

now when i click delete button this code is executed.

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

}

In my html page i have session names as deleted to display the message. But whenever i click delete button the data gets deleted but it show like this. enter image description here

Any Help or suggestion are most welcome.

As per request my view file code is below.Thank you.

    @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

Upvotes: 0

Views: 2858

Answers (5)

kunal
kunal

Reputation: 4248

Try this :-

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

And in view file

@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

Upvotes: 1

Saroj
Saroj

Reputation: 1373

In your Controller Write these code

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

Now In your blade template

@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

Make sure you use these in your controller

use Session;
use Redirect;

I hope this will work for you

Upvotes: 0

Prasanna J
Prasanna J

Reputation: 49

Try below code.

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

Then in the blade

@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

Upvotes: 1

erashdan
erashdan

Reputation: 152

Define deleted variable in session

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

Check session document in laravel website https://laravel.com/docs/5.4/session#flash-data

Upvotes: 1

Rafik Farhad
Rafik Farhad

Reputation: 1202

you can try this

@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

Upvotes: 0

Related Questions