Cannot get Session message from Handler class laravel - php

What I try to do is to get a message in the session when redirecting back to the page from exception handler class, When I get 'PostTooLargeException' It should back to page with a message.
If statement in Handler class
public function render($request, Exception $exception)
{
//...
if ($exception instanceof PostTooLargeException) {
$test = redirect()->route('clientbank.create')->with('message', 'File too large!'); //Cannot get message
return $test;
//dump($gg);
dump(session('message'));
dd('stop');
}
// this will still show the error if there is any in your code.
return parent::render($request, $exception);
}
}
In dump(session('message')); I can see the message .
In blade page
#elseif(session('message'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ session('message') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#endif
In controller
public function create()
{
dump(session('message')); <-- getting null !!!
return view('clinetbank.bank.cratebank');
}
What I try to do is use empyt(session('message') I always get an empty session.
Also, I try this questions but not work for me.
I use laravel 5.8
Any ideas, please ?.

Related

How Do I Add A Status Message to a Reset Password Redirect? (Laravel 5.6)

I'm trying to find a way to add a status message to the default ResetPasswordController redirect, but I can't seem to figure it out. I've tried replacing the default
protected $redirectTo = '/home';
with
public function redirectTo() {
return redirect()->home()->with('status', 'Your password has been changed successfully!');
}
but I was met with an error. I've read on other posts that the default adds a status message already, but I have an area to show the status and nothing pops up. (Any other status the project has, it shows fine)
#if (session('status'))
<div class="form-group">
<div class="alert alert-success alert-dismissible">
<i class="fa fa-times" aria-hidden="true"></i>
{{ session('status') }}
</div>
</div>
#endif
I haven't found any helpful information on this in the documentation either, has anyone found a solution to this?
May be this will help.
public function redirectTo() {
Session::flash('status', 'Your password has been changed successfully!');
return redirect('home');
}
Dont forget to use Session class at top.

Laravel 5 Flash messages not working

My flash messages are not displaying on the view. I have tried post SO questions but didn't worked.
Here is my controller code:
use Session;
//other code
//my code to set flash message and redirect it
\Session::flash('message', $message);
return redirect('admin/groups/add');
My View code:
#if(Session::has('message'))
<div class="alert alert-danger" id="alert_danger">
{!!Session::get('message')!!}
</div>
#endif
I don't know where I am going wrong.
In your controller, add
return Redirect::back()->withErrors('Password is incorrect.');
In view, add
#if($errors->first())
<p class="alert alert-danger">{{$errors->first()}}</p>
#endif
in your controller :
use Session;
public function myfunction(){
return redirect('admin/groups/add')->with('message', $message);
}
in your blade view :
#if(session()->has('message'))
<div class="alert alert-danger" id="alert_danger">
{{{{session('message')}}}}
</div>
#endif

Laravel FormRequest message and integer not working

FormRequest page
class UserSaveRequest extends FormRequest{
....
public function rules()
{
return [
'st_email'=>'required|email',
'st_USN'=>'required|max:15',
'st_phone'=>'max:15',
'st_address'=>'string|max:30',
'st_department'=>'string|max:50',
'st_semester'=>'max:4'
];
}
public function message(){
return [
'required' =>'not null',
'string' =>'string',
'st_USN.max' =>'max15',
'st_phone.max' =>'max15',
'st_address.max' =>'max30',
'st_department.max' =>'max50',
'st_semester.max' =>'max4',
'email' =>'email plz'
];
}
}
In the blade.php page, i just add this code.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
In the controller part
public function saveStudent(UserSaveRequest $request){
.....
}
I wanna show some error message by my setting, but it always not working.
In addition, if i added the condition of the integer, the error message will always show "max" error. In fact, the size of the input data is not over 4.
'st_semester'=>'max:4|integer'
The st semester may not be greater than 4.
By the way, single rule will be allowed.
'st_semester'=>'max:4' or 'st_semester'=>'integer'
Have anyone know how to solve these problems?

Laravel 5.1 Login Session Message

I'm trying to add a Session success message when a User login.
I've tried adding the following to the AuthenticatesUsers.php trait postLogin():
if (Auth::attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles)->withSuccess("message");
}
I've also tried adding to the handleUserWasAuthenticated():
return redirect()->intended($this->redirectPath())->withSuccess("message");
I run composer dump-autoload after each change but it just will not flash the message in the view. I use a partial called success.blade.php and the contents are:
#if (Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>
<i class="fa fa-check-circle fa-lg fa-fw"></i> Success.
</strong>
{{ Session::get('success') }}
</div>
#endif
I think I'm missing something but I can't think what at the moment so hoping for a fresh set of eyes.
Thank you in advance.
Don't use ->withSuccess().
Use ->with('success', 'Success message'), as described in http://laravel.com/docs/5.1/responses#redirecting-with-flashed-session-data, or use the session manager. To access the session manager, you can use the Request object:
$request->session()->flash('success', 'Success message');
See http://laravel.com/docs/5.1/session#flash-data. You can also access the session manager using the Session facade:
Session::flash('success', 'Success message');

Confirmation alert box with Laravel contact form

I am new to Laravel but have managed to get a contact form working and showing validation errors when there are some.
However I do have one problem and have no idea how to handle it in Laravel. When a message is sent (validation rules pass) I would like to display an alert box (Bootstrap style) saying 'Thanks, message has been sent'.
CODE
public function postContact()
{
$formData = Input::all();
// input validator with its rules
$validator = Validator::make(
array(
'name' => $formData['name'],
'email' => $formData['email'],
'subject' => $formData['subject'],
'message' => $formData['message']
),
array(
'name' => 'required|min:3',
'email' => 'required|email',
'subject' => 'required|min:6',
'message' => 'required|min:5'
)
);
if ($validator -> passes()) {
// data is valid
Mail::send('emails.message', $formData, function($message) use ($formData) {
$message -> from($formData['email'], $formData['name']);
$message -> to('info#company.com', 'John Doe') -> subject($formData['subject']);
});
return View::make('contact');
} else {
// data is invalid
return Redirect::to('/contact') -> withErrors($validator);
}
}
How can I achieve this in Laravel 4?
You could use the with method of the Redirect class:
if ($validator -> passes()) {
// data is valid
Mail::send('emails.message', $formData, function($message) use ($formData) {
$message -> from($formData['email'], $formData['name']);
$message -> to('info#company.com', 'John Doe') -> subject($formData['subject']);
});
//Redirect to contact page
return Redirect::to('/contact')->with('success', true)->with('message','That was great!');
} else {
// data is invalid
return Redirect::to('/contact') -> withErrors($validator);
}
You will be redirected to the contact page with the session variables success and message set.
Use them for an alert in your view, e.g. in a Bootstrap Alert:
with Blade
#if(Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Success!</strong> {{ Session::get('message', '') }}
</div>
#endif
without Blade
<?php if(Session::has('success')): ?>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Success!</strong> <?php echo Session::get('message', ''); ?>
</div>
<?php endif; ?>
If you are using them like this you can even provide success alerts, info alerts, or any alert you want to.
When your data is INVALID you use the withErrors() method to pass some data (erros) to your route.
You can use the same process with any kind of data.
For example:
return View::make('contact')->withMessage("Thanks, message has been sent");
This method withMessage() will create a new variable message and store it in the Session for one request cycle.
So, in your view you can access it like this:
#if(Session::has('message'))
<div class="alert-box success">
{{ Session::get('message') }}
</div>
#endif
I assume you are using Bootstrap so this answer will show the message in pop up window (I test it on Laravel 5)
return View::make('contact')->with('message', "Thanks, message has been sent");
Make sure this code will be added in footer
<!-- Show Pop up Window if there is message called back -->
<?php
if(session('message'))
{
echo '<script>
document.getElementById("popup_message").click();
</script>';
}
?>
Add this function in helper.php so you can use it anywhere in your code
function message_pop_up_window($message)
{
$display = '
<a class="popup_message" id="popup_message" data-toggle="modal" data-target="#message" href="#"></a>
<div class="modal fade" id="message" role="dialog">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Messsage </h4>
</div>
<div class="modal-body">
<p>'.$message.'</p>
</div>
</div>
</div>
</div>
</div>';
return $display;
}
Then call the function in you page
{!! message_pop_up_window($message) !!}

Categories