You'll want to echo out the session message in your view.
More info at https://laravel.com/docs/5.6/session#flash-data
@if(Session::has('message'))
{{ Session::get('message') }}
@endif
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
Please or to participate in this conversation.