Laravel 5.8 – From The Ground Up
Lesson 20 of 52 · Laravel 5.8 Tutorial From Scratch – e20 – Flashing Data to Session & Conditional Alerts in View · Education
Laravel 5.8 Tutorial From Scratch – e20 – Flashing Data to Session & Conditional Alerts in View
now that we have our contact form working our next step is to provide some feedback for our users that way the user knows that their message went through I
will fill out the form right now and just show you the current process that we have so you can see what the issue is at this current moment let me just fill out the form here you email message so
if I hit Send message the form submitted we know that the email gets sent but our user has no idea what just happened so we want to give them some feedback so we've been using bootstrap as our CSS
framework and they have these alerts so I'd like to bring one of these in and go ahead and render that if we have some sort of alert to show but as a first step I'm just gonna put it in the layout
file and see what it looks like let's jump back to phpstorm let's go to our layout file layout that blade and right after our navigation let's just add that code in and check it
out on the browser so that would be fine right in there we can say maybe strong success thanks for your message we'll be in touch all right there we go that will work maybe let's make it green
change it to alert success all right so now we have this but of course we don't want to display this every single time I'm just adding it in for now just so we can get our design dialed in so now that
we have that we know we're gonna use this so of course we need to wrap this in some sort of if statements but before we do that let's go to the contact controller and learn how to actually do
it so let's take a look at our float so right now when we hit the send message button we know we hit this store method the store method validates our data if there's any validation errors it goes
back and shows the user those validation errors so if that passes then we pass that data to this new contact form mail mailable class where we actually send an email and then we return a redirect to
contact so we basically redirect back to contact but with this redirect we can actually tag on another method on here and let's just say with and we can say message and then we can put our message
in there let's bring our message from the layout file thanks for your message we'll be in touch let's actually cut it out and bring it in here so thanks for your message we'll be in touch
so now when we redirect we're gonna have this message inside our session but only once right because we're redirecting width and we're only gonna have that once so now we can wrap this in that if
statement let me cut it out and say if we could use this session so if the session has a message that means we need to display our message and if and this is a new blade directive it works
exactly the same way as it would in an if statement in regular PHP so you say at if and when you're done at and if so if you have a message go ahead and echo out session get and get me the message
it's what we want so we're basically diving into our session and saying get me that message and go ahead and output it right here I'm gonna hit save let's go back to Chrome hit refresh and our
success message is gone of course it is so let's go ahead now and submit our form new customer new email message all right let's send message one more time and there we go so now we get thanks for
your message we'll be in touch so this is the basics of flashing to your session I do like that syntax where you have a redirect with however you may see this in a tutorial you may see something
like session flash and then we can basically have the exact same thing we had before I'm gonna cut it out and put it in here instead so you may have this notation instead and just to prove that
it works exactly the same let me submit another form new customer new email message send message and there we go works exact the same way so either one would work if
you want to use the session and just flash through the session and that makes more sense you could do it that way otherwise again you can redirect with message I think both of those read
really well I do like the redirect with message approach a little bit better so I'm actually gonna undo back and put everything how it was I do like this approach better so redirect back to
contact with message and then your message I think that reads really really well if your message is really really long then you can always make this a new line that way it breaks up into a new
line so that's it those are the basics of being able to flash some data that you're only going to use once and what that means is so we have this success message right here but I'm gonna refresh
the page and our message is gone so it only works in the first subsequent request after that that's it it doesn't do it anymore it simply flashes the session then you do that and then you're
done another common thing is to actually hide the form altogether right so if we just submitted this form it really makes no sense for us to display the form again so the way you would do that is
basically using something very similar to this if statement let's try that now let me go to my create method and I can wrap this entire form in an if statement and say if and then we're going to
negate if this session has a message so we're basically saying the opposite of that if the session does not have a message then display my form okay so now we know we have the opposite so
this form will only display if there is no message let's check out the difference now let's go back to Chrome let me fill out this form new email message all right send message and there
we are thanks for the message we'll be in touch but there is no form obviously from a design perspective this is very crude you wouldn't normally do it like this for production you'd want to make
it as pretty as you possibly can what we're trying to illustrate is how you can flash your session and use those to display or certain messages once we start looking
at view and view components this would be a great example for us to start using some view components so once we get into our front end and laravel section of this course we'll take a look at that
but for now now you know how to flash the session and how to display items on your page whether that key is present in your session or not
Now that our contact form is sending emails, it’s time for us to focus on providing some feedback for our users once the form goes through. Follow along as we flash some data to the session and conditionally display an alert on the view. We will also tackle the inverse to hide our form.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!