Flash Message with Laravel 7/6 Tutorial

Laravel 6 , Laravel , Laravel 7

Nicesnippets

7441

21-12-2019


Hi Guys,

Today, I will show you how to use redirect with flash messages without any package in Laravel application. In this example i am not use flash messages package.

Flash messages is required in laravel application because that way we can give alter with what progress complete, success, warning etc. In this example we use bootstrap because alert flash message design availbale in bootstrap.

In this blog i am added flash message like redirect with success message, error message, warning message and info message.

Step 1 : Create flash-message blade file

In this step you will create flash-message.blade.php file. In this file we will write code of alert bootstrap and check which messages come.

There are following alert will added:

1)success

2)error

3)warning

4)info

5)validation error

resources/views/flash-message.blade.php

@if ($message = Session::get('success'))

<div class="alert alert-success alert-block">

<button type="button" class="close" data-dismiss="alert">×</button>

<strong>{{ $message }}</strong>

</div>

@endif

@if ($message = Session::get('error'))

<div class="alert alert-danger alert-block">

<button type="button" class="close" data-dismiss="alert">×</button>

<strong>{{ $message }}</strong>

</div>

@endif

@if ($message = Session::get('warning'))

<div class="alert alert-warning alert-block">

<button type="button" class="close" data-dismiss="alert">×</button>

<strong>{{ $message }}</strong>

</div>

@endif

@if ($message = Session::get('info'))

<div class="alert alert-info alert-block">

<button type="button" class="close" data-dismiss="alert">×</button>

<strong>{{ $message }}</strong>

</div>

@endif

@if ($errors->any())

<div class="alert alert-danger">

<button type="button" class="close" data-dismiss="alert">×</button>

Please check the form below for errors

</div>

@endif

Step 2 : use flash-message file in theme

In this step we have to just include flash-message.blade.php file in your theme default file. So we can add file like this way:

resources/views/app.blade.php

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Styles -->

<link href="/css/app.css" rel="stylesheet">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>

</head>

<body>

<div id="app">

@include('flash-message')

@yield('content')

</div>

<!-- Scripts -->

<script src="/js/app.js"></script>

</body>

</html>

Step 3 : use flash messages with redirect

In this step we will learn how to give message when you redirect one by one:

1. Redirect with success message

We can simple redirect route or redirect url or redirect back with success flash message, we can use in controller like this way:

public function create(Request $request)

{

$this->validate($request,[

'title' => 'required',

'description' => 'required'

]);

$items = Post::create($request->all());

return back()->with('success','Post created successfully!');

}

2. Redirect with error message

You will validation error redirect back with error then show error message.

public function create(Request $request)

{

return redirect()->route('home')

->with('error','You have no permission for this page!');

}

3. Redirect with warning message

We can simply redirect url or redirect with warning with flash message.

public function create(Request $request)

{

return redirect()->route('home')

->with('warning','Don't Open this link);

}

4. Redirect with info message

We can simple redirect route or redirect url or redirect back with info flash message.

public function create(Request $request)

{

$this->validate($request,[

'title' => 'required',

'description' => 'required'

]);

$posts = Post::create($request->all());

return back()->with('info','You added new posts, follow next step!');

}

5. Validation Error

If you use laravel 5 validation then you will redirect back with errors automatically, At that time it will also generate error flash message.

public function create(Request $request)

{

$this->validate($request,[

'title' => 'required',

'details' => 'required'

]);

.....

}

It will help you...


Recommended Posts