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
Related
Application doesn't show messages as it should.
I tried returning back with message in RoleController. Redirecting works fine, but I can't see any messages. There is multiple functions like this, none of them show messages.
public function store(Request $request)
{
$validated = $request->validate(['name' => ['required', 'min:3']]);
Role::create($validated);
return back()->with('message', 'Role created successfully.');
}
And this code below is in admin.layout blade.
#if (Session::has('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
{{ Session::get('message') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
#endif
I googled this many many times but can't find a solution. This worked fine month ago, but when I continued with this yesterday, it suddenly stopped working. Php version hasn't changed, it's 8.0.2. I also tried flash-messages with no help.
Sorry, not a native english speaker.
edit / I have also cleared cache with php artisan.
try this
In Controller
return redirect()->route('admin.listing')->with('message', 'Role created successfully.');
In Blade file
#if (Session::has('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
{{ Session::get('message') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
#endif
I think you can try this...
#if ($message = Session::get('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
<strong>{{ $message }}</strong>
</div>
#endif
Please try one time with this one
return redirect()->back()->with('message', 'Role created successfully.');
Now it's working. I needed to modify Kernel.php and remove \Illuminate\Session\Middleware\StartSession::class, under the protected $middlewareGroups.
I am using Laravel v8.16.1 and PHP v7.3.1 when I am trying to get a message in the blade template.
#if(session('error'))
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h5><i class="icon fas fa-ban"></i> Success!</h5>
{{ session('success') }}
</div>
#endif
Using the below code, it's working for me.
$req->session()->flash('error', 'Invalid Username or Password');
return view('admin.views.login');
However, it's not the right way, and when I am trying to redirect back using the below code, then it's not working
return redirect()->back()->with('error', 'Invalid Username or Password');
Please help what is missing or what is wrong?
You can use to show errors
#if(count($errors) > 0 )
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<ul class="p-0 m-0" style="list-style: none;">
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
And the redirect code would be
return redirect()->back()->withErrors(['errors' => 'There is some error in the details.']);
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
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
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.