Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

valhallathompson's avatar

Session global variable not showing in my view?

Hi Everyone

I created a search function on my website. When a user searches and zero results appear I want a message to appear that says "no results found. try again"

I attempted to do this via laravel 4 session variable. I created a variable called "global". I'm trying to echo this variable out onto my view but it's not working. My question is, what am I doing wrong? How can I get my message to appear on my website?

My Controller

    public function postUseSearch(){

        $searchString = Input::get('keyword');

        $string = str_replace(',', '', $searchString);
        $string = str_replace('\'', '', $string);

        $insert                         = new Searchword();

        $useTheFunction                 = new theClass();
        $keyword                        = $useTheFunction->searchNames($string);

        if(count($keyword) > 0){
            $insert->keyword            = $string;
            $insert->blank              = '1';
            $insert->save();

            return View::make('search.searchresults')
                    ->with('keyword', $keyword);

        } else {
            $insert->keyword            = $string;
            $insert->blank              = '0'; 
            $insert->save();

            return View::make('search.searchresults')
                    ->with('global', 'no results found. try again');
        }
}

My View

    @if((Session::get('global')))
        <p>{{ Session::get('global') }}</p>
    @endif
0 likes
2 replies
thepsion5's avatar

Data passed to the view via the with() method doesn't use the session, it's just passed directly. This should work with your posted controller code:

@if( isset($global) )
    <p>{{ $global }}</p>
@endif
1 like
Ozan's avatar

You can check the session by using Session::has('global') method.

So your blade view should be like this:

@if(Session::has('global'))
    <p>{{ Session::get('global') }}</p>
@endif

Please or to participate in this conversation.