1

I did as directed according to the answer provided to this question but it didn't work for me . So i reasked this question. my controller is

public function save() {

        $med_group = MedicineGroup::create(Request::all());

        if ($med_group) {
            $this->setServerMessage('MedicineGroup created successfully');
            return Redirect::to('admin/user/create')->with('flashmessage',$this->getServerMessage());

        }
    }

I have made setServerMessage() and getServerMessage() in Controller.php

public function setServerMessage($messagearray) {
        if (is_array($messagearray)) {
            $type = $messagearray['type'];
            $html = "<div style='height:auto;padding:7px 0px 6px 20px;margin:0px' class = 'pull-left text-left col-md-8 alert alert-{$type}'>{$messagearray[0]}</div>";
        } else {
            $html = "<div style='height:33px;padding:7px 0px 6px 20px;margin:0px' class = 'pull-left text-left col-md-8 alert alert-info'>{$messagearray}</div>";
        }
        $temp = '';
        if (\Session::get('SERVER_MESSAGE')) {
            $temp = \Session::get('SERVER_MESSAGE');
        }
        \Session::put('SERVER_MESSAGE', $temp . $html);
    }

public function getServerMessage() {

        if (\Session::get('SERVER_MESSAGE')) {
            $temp = \Session::get('SERVER_MESSAGE');
            \Session::forget('SERVER_MESSAGE');
            return $temp;
        } else
            return "";
    }

my view is setup like this

<div class="box-footer text-right">
                @include('flash')
                <input type="submit" class="btn btn-success" value='Save'>
                <input type="reset" class="btn btn-primary" value='Reset' />
                <a href="#" class="btn btn-danger">Cancel</a>
            </div>

and in my flash.blade.php i have written

@if(isset($flashmessage))
   {!! $flashmessage !!}
@endif

what did I miss? i followed this site too but i can't flash a message in my view.

Community
  • 1
  • 1
Saurab
  • 1,931
  • 5
  • 20
  • 33

3 Answers3

1

Set the flash message and then redirect to desired route

Controller:

  session()->flash('msg', 'Successfully done the operation.');
  return Redirect::to('admin/user/create');

Now get the message the in view blade file

Blade

 {!! Session::has('msg') ? Session::get("msg") : '' !!}
Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
0

Try out this thing, I am replacing your entire code, you can change it according to your need:

public function save()
{
    $med_group = MedicineGroup::create(Request::all());

    if ($med_group) {
        //$this->setServerMessage('MedicineGroup created successfully');

        // flash method to be used when just printing the message
        // on the screen. Link below
        \Session::flash('key', 'Your message here...');

        return Redirect::to('admin/user/create');

    }
}

In your view file:

@if(Session::has('key))
    {{ Session::get('key') }}
@endif

Link for more information

Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71
0
every other thing seems to be fine except for your layout 

laravel would escape the variable you are passing into the session while using this 

@if(isset($flashmessage))
   **{!! $flashmessage !!}**
@endif

you should do this instead 

@if(isset($flashmessage))
   **{{ $flashmessage }}**
@endif
osleonard
  • 595
  • 5
  • 22
  • nope .. didnot work . If I do dd($this->getServerMessage()) inside Controller I can see my flash message but Why can I not print that very message inside flash.blade.php And So strange thing is I **cannot print** it even inside create() function on same Controller where I am redirecting it to. – Saurab Mar 01 '16 at 19:23
  • this is why you wont get the message \Session::forget('SERVER_MESSAGE'); your are removing the message from the session read more about sessions here https://laravel.com/docs/5.0/session – osleonard Mar 02 '16 at 02:11