Home > Software design >  Unable to display flash message using laravel
Unable to display flash message using laravel

Time:08-09

I am new in laravel and i am working on laravel (version 8 ) trying to save "form data" into database,but i am faceing following problems

  1. How can i display flash message ? right now my "success" message is working fine but "error" message is not displaying
  2. I just created "model" file,without define any "table" name in this file, but data is saving into database,is this right ? actually i want to know to purpose of "create model" because without mention "table" name,data is saving into database

Here is my controller code

   <?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Students;
class StudentController extends Controller
{
    function addStudent(Request $req)
        {
                $Students= new Students;
                $Students->name=$req->name;
                $Students->email=$req->email;
                $Students->marks=$req->marks;
                $Students->address=$req->address;
                $saved=$Students->save();
                try
                    {
                        Students->save();
                    }
                catch(\Exception $e)
                    {
                        return Redirect::back()->withErrors(['error2' => 'something went wrong!']);
                    }
                return back()->with('success2','User created successfully!');
        }
}

Here is my view file

 @if (\Session::has('error2'))
                <div >
                    <a href="#"  data-dismiss="alert">&times;</a>
                    <strong>{!! \Session::get('msg') !!}</strong>
                </div>
                @endif
              
              <form  action="addStudent" method="POST">
              @csrf
                    //my form code here
                  </form>

CodePudding user response:

How can i display flash message ? right now my "success" message is working fine but "error" message is not displaying

Instead of

return back()->with('error2','something went wrong!');

Try

function addStudent(Request $req)
    {
        $Students= new Students;
        $Students->name=$req->name;
        $Students->email=$req->email;
        $Students->marks=$req->marks;
        $Students->address=$req->address;
        try
        {
            $Students->save();
        }
        catch(\Exception $e)
        {
            return back()->withErrors(['error2' => 'something went wrong!']);
        }
        return back()->with('success2','User created successfully!');
    }

and inside your view call this

@if($errors->any())
    <div >
        <a href="#"  data dismiss="alert">&times;</a>
        <strong>{{ $errors->first() }}</strong>
    </div>
@endif

If you want to get the single error2 here then you can use

@if (\Session::has('error2'))
    <div >
        <a href="#"  data dismiss="alert">&times;</a>
        <strong>{!! \Session::get('error2') !!}</strong>
    </div>
@endif

I just created "model" file,without define any "table" name in this file, but data is saving into database,is this right ? actually i want to know to purpose of "create model" because without mention "table" name,data is saving into database

You don't need to define a table name in your because laravel can auto link your table to your model if you follow the laravel naming convention for naming your model and table. According to laravel if your model name is User then laravel will bind the table with name users to this model automatically without you defining the table name in the model that is table name should be plural of the model name and in all lower case.

In above case if you want to use the table name other than users then you will have to explicitly define the table name in the model so that laravel can bind this table to the model.

  • Related