Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e20 – Flashing Data to Session & Conditional Alerts in View

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

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!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue