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

scala's avatar
Level 1

print dynamic message in session variable

I am trying to insert data in database but not successful. I want to print the error message, why data is not inserting in the database via session variable. How can I print error message via session variable in another page?

<form class="form-horizontal" method="POST" action="{{action('BookController@store')}}">
                {{ csrf_field() }}
        <div class="row" style="padding-left: 1%;">
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Book Name</label><span class="required">*</span>
                        <input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
                    </div>
                    <div class="form-group">
                            <label>Book ID</label><span class="required">*</span>
                            <input type="text" maxlength="10" minlength="2" required="required" runat="server" id="txtBookId" class="form-control" autocomplete="off" />
                    </div>
                    <div class="form-group">
                            <label>Unit Price</label><span class="required">*</span>
                            <input type="text" maxlength="5" required="required" runat="server" id="txtBookUnitPrice" class="form-control" onkeypress="return decimalOnly(event)" autocomplete="off" />
                    </div>
                    <div class="form-group">
                            <button type="submit" class="btn btn-primary">Submit</button>        
                    </div>            
                </div>
        </div>
</form>

Controller code

    public function store(Request $request)
    {     

        $validatedInput = $request -> validate([
            'txtBookName' => 'required|string|min:3|max:100',
            'txtBookId' => 'required|string|min:2|max:10|unique:books,BookID',  // unique:table_name,column_name
            'txtBookUnitPrice' => 'required|max:5|numeric'
        ]);

        $book = new Book;
        $book -> BookName = $request->input('txtBookName');
        $book -> BookID = $request->input('txtBookId');
        $book -> BookUnitPrice = $request->input('BookUnitPrice');
        $book->save();

        Session::flash('message', 'this is about page');
        return view('pages.about');
    } 

I want to print the error or success message in the session of about page. How can I do this?

0 likes
4 replies
Cronix's avatar

You're already using form validation. Why not just display the messages that it triggers? It will be a lot more specific and useful to the user than "didn't work". "The book name is required", "The book name must be between 3 and 100 characters", etc.

The form errors are already automatically stored in session. You just need to access them and show them.

Are you using ajax or something to submit your form? None of your form fields have name attributes, so they won't be sent.

https://laravel.com/docs/5.6/validation#working-with-error-messages

Snapey's avatar

at the end of the store method redirect to a route that will show a view - dont return the view in store method

Cronix's avatar

You can just display all validation errors, or a success message, like

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@else
    <div class="alert alert-success">Your data was successfully saved</div>
@endif 

Please or to participate in this conversation.