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.
Related
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.']);
What I want:
I want to display a message when the user is redirected back to the form page after a mail is sent.
What I am doing
Here is the code in my SendEmailController.php:
return back()->with('success', 'Mail Sent !');
Here is my code in my view/index.blade.php:
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
#endif
I want to mention that I just followed this tutorial: https://www.webslesson.info/2018/09/simple-way-to-sending-an-email-in-laravel.html
In practice :
Nothing is displayed, the main page remain the same and my mail is sent.
What i tried :
I tried setting a route as following:
In my web.php:
Route::get('/succeed', 'PagesController#succeed')
In my PagesController.php:
return view('index')->with('success', 'Mail Sent !');
When I go to http://localhost/succeed, my div is displayed.
In the end, I don't understand why I don't understand why that div don't want to show up.
Edit:
Tried:
redirect()->back()->with('success', 'Mail Sent !');
Not fixing the issue
My problem is fixed, I had some AJAX in the background that was blocking the redirection.
Thanks you a lot for giving me answers.
Like said above, use redirect()->back()->with('success', 'Mail Sent !');
then
use session('success')
#if (session('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
#endif
I create an API that work like punch card machine. So when the GET API is requested, it should the result if access granted or access denied on login screen.
I'm running Laravel 5.7 with PHP 7.2. I already dump the session() variable, only cross ref token is shown.
api.php
Route::get('/{member_id}', 'ApiController#entry');
ApiController.php
public function entry($member_id)
{
$user = User::where('member_id', $member_id)->first();
if ($user == null)
return redirect('login')->with('api_error','Access Denied');
else
{
return redirect('login')->with('api_success','Entry Recorded');
}
}
login.blade.php
#if (session('api_success'))
<div class="alert alert-success alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
{{ session('api_success') }}
</div>
#endif
#if(session('api_error'))
<div class="alert alert-danger alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
{{ session('api_error') }}
</div>
#endif
Nothing is output after run the api link.
Extra Info : the login are scaffolding from php artisan make:auth
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.
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