Session flash message timeout in Laravel - php

I have created flash message in Laravel page using controller. It's showing well but need to add timeout in flash message
if($location_vaidation>0){
$material_details->location_id=$requested_location;
}
else{
Session::flash('success', 'please fill the form with valid data');
return Redirect::to('request');
exit;
}
In view page
#if( Session::has("success") )
<div class="alert alert-success alert-block" role="alert">
<button class="close" data-dismiss="alert"></button>
{{ Session::get("success") }}
</div>
#endif
#if( Session::has("error") )
<div class="alert alert-danger alert-block" role="alert">
<button class="close" data-dismiss="alert"></button>
{{ Session::get("error") }}
</div>
#endif
<div class="flash-message"></div>

Try this using Jquery function
$("document").ready(function(){
setTimeout(function(){
$("div.alert").remove();
}, 5000 ); // 5 secs
});

you might want this to autoclose / fadeout your alert messages, This will be a smooth fading , and you do require jquery
$(".alert").fadeTo(2000, 500).slideUp(500, function(){
$(".alert").slideUp(500);
});

In the header add this:
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine#2.8.2/dist/alpine.min.js"></script>
then use it in blade as following:
#if (session($messageKey))
<div x-data="{show: true}" x-init="setTimeout(() => show = false, 5000)" x-show="show">
<div class="alert alert-success">
{{ session($messageKey) }}
</div>
</div>
#endif

Related

Success message is not showing after submitting form

Laravel Controller
my insert data method in controller
$category = array(
'name' => $request->name,
'code' => $request->code,
'image' => $image_name
);
Category::create($category,$image_name);
return Redirect::to('/category')->with('success','Record
inserted successfully');
laravel view
index view where I want to display message
#if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
#endif
Route.php
Route::resource('/category', 'CategoryController');
Try something like this:
<div class="panel panel-default">
#if (session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
#endif
</div>
try:
#if(session()->has('success'))
<div class="alert alert-dismissable alert success" style="background: white;">
<button type="button" class="close" data-dismiss="alert" aria-label="close">
<span aria-hidden="true">×</span>
</button>
<strong>
{!! session()->get('success') !!}
</strong>
</div>
#endif
Try this, is should work.
Route
Route::get('category', 'CategoryController#index')->name('category');
$category = array(
'name' => $request->name,
'code' => $request->code,
'image' => $image_name
);
Category::create($category, $image_name);
return redirect()->route('category')->with('success', 'Record inserted successfully');
Success Message View
#if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
#endif
Hi every one and thanks alot for replying me I got solution to my stated problem from below link
Laravel 5.2 Validation Error not appearing in blade

Redirect to login page with message not working in laravel 5.3

When registering for external link, I want to dislay a message :
if (!Auth::check()){
Session::flash('message', trans('msg.please_create_account_before_playing', ['tournament' => $tournament->name]));
return redirect(URL::action('Auth\LoginController#login'));
}
In my login page, I have:
#if (Session::has('message'))
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
{{ Session::get('message') }}</div>
#endif
I checked in my login page, with laravel debugbar, and there is no message var in session.
But it never shows up...
Any idea why it is not working?
Try dd(Session::has('message')); within the login method on your LoginController. I suspect this will return true, if so assign it to a variable of message then pass that variable to the view.
Along the lines of:
public function login() {
$message = Session::has('message') ? Session::get('message') : '';
return view('login', compact('message'));
}
Then in your view:
#if (!empty($message))
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
{{ Session::get('message') }}</div>
#endif
If the above does not work, I suspect this may be related to middleware.

Get the key of a flash message

I am working with Laravel and I am new. I jsut set a flash message by using this line of code: session()->flash('status', 'This is my flash message to display');
To retrieve the message I use session('status').
Now my question is, is there any possibility to get the key of the flash message? In my example, the key of the flash message is status
Set an array of data in session with type and message.
session()->flash('message', [
'type' => 'success',
'body' => 'This is my flash message to display'
]);
Then you can access the message type like
session('message.type')
In your blade view you can do this to have a dynamic alert message
#if (session()->has('message'))
<div class="alert alert-{{ session('message.type') }}">
{{ session('message.body') }}
</div>
#endif
You can get an array of all keys of newly flashed values using:
session('_flash.new');
Pass message like this
return redirect()->back()->with('success', 'Destination deleted successfully');
Use like this
#if(Session::has('success'))
<div class="alert alert-success alert-dismissable alert-box">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ Session::get('success') }}
</div>
#endif
#if(Session::has('error'))
<div class="alert alert-danger alert-dismissable alert-box">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ Session::get('error') }}
</div>
#endif

Laravel : Error messaging not working

Hello friends I have a form when i enter correct value and pressing submit button i am getting success message
if i am entering wrong value then
i am not getting error message please suugest something
Controller
if($getHours <= 16)
{
DB::table('labors')->insert($labors_data);
session::flash('status', 'Labor Timesheet Added Successfully');
return Redirect:: to('ViewLaborD2S');
}
else
{
session::flash('status', 'You have entered More than 16 Hours');
return Redirect:: to('ViewLaborD2S');
}
View
#if (session('status'))
<div class="alert alert-success">
<p style="text-align:center;">{{ session('status') }}</p>
</div>
#else(session('message'))
<div class="alert alert-danger">
<p style="text-align:center;">{{ session('message') }}</p>
</div>
#endif
I want to show Success message if i entered correct value
and if i enter wrong value show error message
Try this:
#if (Session::has('status'))
<div class="alert alert-success">
<p style="text-align:center;">{{ Session::pull('status') }}</p>
</div>
#endif

Laravel 4 Redirect with global styling

Does anyone know where and how to style the redirect with global such as:
return Redirect::route('home')
->with('global', 'Your content has been posted!');
I want to use the bootstrap alerts with the global message
I just needed to put the styling after the global
Here is what I did:
return Redirect::route('admin-console-post')
->with('global', '
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
Your content has been posted successfully!
</div>
');
You need to style it in view like this:
#if(Session::has('global'))
<div class="output">
{{ Session::get('global') }}
</div>
#endif
Where output is class in which you put style of your choice.
Hope it helps ^^.

Categories