Laravel : Error messaging not working - php

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

Related

Session flash message timeout in Laravel

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

flash message if user is not login laravel

When user tried to go user dashboard without login, it return to login page. it works perfectly. but I need to show message on login page using middleware that 'please login to see this page.'
I tried {!! $errors->first('loginpermission', ':message') !!}
But it not working
Please help me how to use this.
Add these codes in your login.blade page
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
#if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
#endif
Add below code in your middleware at condition when user is not logged in
return redirect('/login')->with('error', 'please login to your account');
#if ($errors->has('loginpermission'))
{{ $errors->first('loginpermission') }}
#endif

Laravel is displaying error message before form submit

in my last question i was asking for help with single error message displaying after incorrectly filled form instead of error for every incorrect input. They were wery helpfull and I received this piece of code:
#if ($errors)
<span class='help-block'>
<strong>{{ "There are errors" }}</strong>
</span>
#endif
and the second answer was
#if (!empty($errors))
<span class='help-block'>
<strong>{{ "Some input field is not properly filled" }}</strong>
</span>
#endif
I thought that the above will be displayed only after form submiting and if Laravel will find any errors. The problem is that it's always displaying error message like in the screenshot under:
Does anyone knows the solution? Many Thanks.
$errors variable is always set. It's a message bag that might or might not contain messages.
You could check like this:
#if ($errors->count() > 0)
<span class='help-block'>
<strong>{{ "Some input field is not properly filled" }}</strong>
</span>
#endif
#if (!$errors->isEmpty()) should also work.
So if I understood it correctly, you want to show the success or error message only after submitting your form.
If that is the case following should work:
You need to add / change following in your Controller function:
$request->session()->flash('success', 'Success!');
return redirect()->route("yourstuff.index");
In your blade.php you return that session with it message like this
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
#if (session('error'))
<div class="alert alert-success">
{{ session('error') }}
</div>
#endif
The problem is solved, i received two good answers but i choosed simplier one.
In order to return single error I used the code proposed by user:
devk
#if ($errors->count() > 0)
<span class='help-block'>
<strong>{{ "Some input field is not properly filled" }}</strong>
</span>
#endif
And works great.I want to thank to user:
utdev
For this code:
You need to add / change following in your Controller function:
$request->session()->flash('success', 'Success!');
return redirect()->route("yourstuff.index");
In your blade.php you return that session with it message like this
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
#if (session('error'))
<div class="alert alert-success">
{{ session('error') }}
</div>
#endif
I will use the code proposed by you in other part of my project.
THANKS !

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.

Laravel 5.1: with() helper is not working

I am using with() helper to pass some error messages to views. My code for this is
redirect('somewhere')->with('message', 'show some message')
Then in the targeted view to catch the message I have this:
#if(count($errors)>0)
<div class="alert alert-success">
<ul >
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
But no message is delivered to the view. What's the problem here?
redirect('somewhere')->withErrors(['message', 'show some message'])
Please check this link Redirecting With Flashed Session Data
In targeted view you can handle message like this
#if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif

Categories