AppDividend
Latest Code Tutorials

Laravel Flash Message Example

714

Get real time updates directly on you device, subscribe now.

Laravel Flash Message Example is the topic, we will discuss today. We will use mercuryseries/flashy Package for creating Flash Message in Laravel. This Package provides a different message like success message, error message, info message, primary message, warning message. You can use redirect back with() flash message in laravel.  If You get more information then Go To Github.

Laravel Flash Message Example

We are going to Configure Laravel Project.

Step 1: Install Laravel Project

Download New Laravel Project by the writing following command.

composer create-project --prefer-dist laravel/laravel laravelflashy

Step 2: Install Flashy Package

So now let’s install mercuryseries/flashy Package in our laravel application.

composer require mercuryseries/flashy

Step 3: Define providers and aliases

Find the providers in config >> app.php file and register the FlashyServiceProvider.

'providers' => [
        // ...
       MercurySeries\Flashy\FlashyServiceProvider::class,
    ]

Fix the aliases in config >> app.php file and register the aliases.

'aliases' => [
        // ...
        'Flashy' => MercurySeries\Flashy\Flashy::class,
    ]

Step 4: Setup a MySQL database

Now, set the database in the .env file.

//.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelflashy
DB_USERNAME=root
DB_PASSWORD=

I have set local database credentials.

Step 5: Create a model and migration file

Type the following command in your terminal.

php artisan make:model Mobiledetail -m

It will create two files.

  1. Mobiledetail.php model.
  2. create__mobiledetails_table migration file.

We need to create Schema for the mobiledetail table. So navigate to Laravel  >>  database  >>  migrations  >> create__mobiledetails_table

//create_mobiledetails_table
 
public function up()
    {
        Schema::create('mobiledetails', function (Blueprint $table) {
            $table->increments('id');
            $table->string('mobilecompany');
            $table->string('model');
            $table->integer('price');
            $table->timestamps();
        });
    }

In the database, you can see the mobiledetail table.

Step 6: Create a view file

Create a file in resources  >>  views  >>   addmobile.blade.php and put this following code in it.

<!-- addmobile.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel Flash Message Example</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">  
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  </head>
  <body>
    <div class="container">
      <h2>Laravel Flash Message Example</h2><br/>
      <form method="post" action="{{url('mobile')}}">
        @csrf
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="Mobile Company">Mobile Company</label>
            <input type="text" class="form-control" name="mobilecompany">
          </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="Model">Model:</label>
              <input type="text" class="form-control" name="model">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="Price">Price:</label>
              <input type="text" class="form-control" name="price">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <button type="submit" class="btn btn-success">Submit</button>
          </div>
        </div>
      </form>
   </div>
   <script src="//code.jquery.com/jquery.js"></script>  
    @include('flashy::message')
  </body>
</html>

Step 7: Create one controller and route 

php artisan make:controller MobileController

It will create one controller file called MobileController.php

we register route in routes  >>  web.php file. So let us do it.

//web.php

Route::get('mobile','MobileController@create');
Route::post('mobile','MobileController@store');

Add code to create() function to display view.

//MobileController.php

public function create()
    {
        return view('addmobile');
    }

Step 8: Save Data into Database

We need coding the store function in series to store the data in the database.

//MobileController.php

use App\Mobiledetail;

public function store(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'mobilecompany' => 'required',
            'model' => 'required',
            'price' => 'required',
        ]);
        if ($validator->fails())
        {
            flashy()->error('Please Insert Value');
            return back() ;
        }
        $mobile=new Mobiledetail();
        $mobile->mobilecompany=$request->get('mobilecompany');
        $mobile->model=$request->get('model');
        $mobile->price=$request->get('price');
        $mobile->save();
        flashy()->success('Mobile has been succesfully added');
        return back();
 }

Final Code of MobileController.php looks like that.

//MobileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mobiledetail;

class MobileController extends Controller
{
    public function create()
    {
        return view('addmobile');
    }
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'mobilecompany' => 'required',
            'model' => 'required',
            'price' => 'required',
        ]);
        if ($validator->fails())
        {
            flashy()->error('Please Insert Value');
            return back() ;
        }
        $mobile=new Mobiledetail();
        $mobile->mobilecompany=$request->get('mobilecompany');
        $mobile->model=$request->get('model');
        $mobile->price=$request->get('price');
        $mobile->save();
        flashy()->success('Mobile has been succesfully added');
        return back();
   }
}

Start Laravel Development Server by the hit command in cmd.

Related Posts
1 of 37
php artisan serve

Type Below URL on your browser.

http://localhost:8000/mobile

When the data is successfully inserted into the database then success message looks like that.

Laravel 5 Flash Message Example

If any validation fails then error message looks like that.

Laravel Flash Message

If you provide some information to a user then use info message looks like that.

flashy()->info('Welcome Back To Appdividend');

 

Flash Messages in Laravel

If you warn the user then use warning message look like that.

flashy()->warning('Something Wrong');

Flash Messages in Laravel 5

 

If you give some basic information to the user then use primary message look like that.

flashy()->primary('Move To Next Step');

Laravel Redirect Back with Flash Message

Finally, Our Laravel Flash Message Example is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.