Ak Ak
Ak Ak

Reputation: 81

isset is not working in laravel

I am sending a variable from controller to a view.

$message = "Thanks for Contacting";

 return redirect('services')->with($message);

HTML

 @isset ($message)
 <a style="color: red;"> {{$message}} </a>
@endisset

But it shows en error, because when the first services is loaded from this route Route::get('/services', function () { return view('services'); });

There's no variable,so it gives en error. Please let me know what I am missing?

Upvotes: 0

Views: 808

Answers (3)

Aakash Tushar
Aakash Tushar

Reputation: 504

The problem is how you're passing the variable to your view.

the correct way is:

return view('services')->with('message', $message);

or

return view('services')->withMessage($message);

or

return view('services', ['message' => $message]);

or

return view('services', compact('message'));

Upvotes: 2

Supun Praneeth
Supun Praneeth

Reputation: 3160

You have to use it with @if, Try this:

@if(session()->has('message'))
 <a style="color: red;"> {{ session('message')}} </a>
@endif

and also change your controller like this:

return redirect('services')->with('message', $message);

Upvotes: 1

Dharma Saputra
Dharma Saputra

Reputation: 1674

The variable that set in redirect()->with() will saved to sessions variable, so to access it just call session() helper. Example:

@if (session('message'))
    <div class="alert alert-success">
        {{ session('message') }}
    </div>
@endif

More explanation: https://laravel.com/docs/5.6/redirects#redirecting-with-flashed-session-data

Upvotes: 0

Related Questions