简体   繁体   中英

My alert is not showing in laravel 5

this is my PostController.php,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Depress;
use App\Http\Requests;

class PostController extends Controller
{
    public function createPost(Request $request)
    {

        $depress = new Depress();
        $depress->depression = $request['depression'];
        if($request['name'])
            $depress->name = $request['name'];
        else
            $depress->name = "Depressed Anonymous";
        $depress->save();
        \Session::flash('info','Success');
        return redirect()->route('home');

    }
}

And This is my alert.blade.php,

@if(Session::has('info'))
    <div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        {{ Session::get('info') }}
    </div>
    <br>
@endif

And, I have included it in my layout.blade.php

But, no alert shows up...

thanks in advance

No need to use session flash. use the laravel way,

In your controller

return Redirect::to('home')->with('success', 'You message here.');

In your view

@if (isset($success))
<div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          {!! $success !!}. 
</div>
@endif

Change this line:

\\Session::flash('info','Success'); return redirect()->route('home');

To:

return redirect()->route('home')->with(['info' => 'Success']);

Basically, this will redirect you to home. So you need to make sure that in the action(function) that handles the route home , it returns to the view alert.balde.php or to a view that uses alert.balde.php . I tried doing this and it worked for me. Here is my code below:

in my controller:

public function home()
{
    return view('welcome');
}

public function test()
{
    return redirect()->route('home')->with(['info' => 'Success']);
}

And in my welcome.blade.php :

@if(Session::has('info'))
    <div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        {{ Session::get('info') }}
    </div>
@endif

And I get to see 'Success' in the view.

Hope this helps.

Use compact() on return and pass that variable in it. And you can access it as normal php variable in view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM