Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e10 – Adding Email For Customers

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

Laravel 5.8 Tutorial From Scratch – e10 – Adding Email For Customers

in this episode we're gonna be talking about adding an email to our database and being able to store our customers emails not just the name as we've been

doing so far so if you gave this a go in your own hopefully you came to a solution that actually worked and you were able to save an email and the last thing we would want to do is probably

have it displayed after a customer's name but for those of you that didn't let's start from scratch so I could show you how to do it back to phpstorm so the first thing we're going to do is

we need to modify our migration right so remember the migration is what describes our database now you could have taken several approaches to this during the development of your project you're going

to be constantly changing migrations and as long as you don't deploy your migrations to a production server this is perfectly fine you can keep modifying your migrations as many times as you

need again once you kind of ship the version of your project from there on out you're gonna have to generate a new migration but since we have not shipped anything we can go back to our create

customers migration and let's add a new string and we're gonna call it email okay so so far so good so we have a name and now we have an email so back in the terminal we

actually need to rollback that migration because it already ran so we need to step backwards one migration and then migrate the database again so we'll say PHP artisan migrate rollback and well

rollback will do again is take one step backwards and when we see that we see that we rolled back this create customers table and now PHP artisan migrate and now when we migrate forward

now weary migrate this customers table except that this time our change to the migration which was to add this email column is now there okay so that is the part for the database the part for our

controller is that we need to add a second field here so the field we're adding is going to be an email and for now we're just gonna say it is required we

making a new customer we're giving it a name and then let's give it an email so customer email is equal to request email all right then finally in our customer that blade file we need to add a new

input so add this here and we'll say the name for this is email okay so far so good now one thing we don't have here is we don't really have any labels so let me

add just a pee tag here with name and let me add another one here for email just to clean it up a little bit let's go back to my form hit refresh and there we go

it looks like my email is basically touching my name so let's add a little bit of spacing at the bottom let's just say PB 2 that's padding bottom two and there we go we just have a little bit of

space so if we hit add customer of course it says wait a minute the name field is required let's say John Doe and then hit add customer and then we don't really get any errors but

we see that nothing got added to the database and this may seem a little strange at first but it's really not let's check out our blade one more time in our errors we are fetching the first

error for name and not for email so we actually need another one just for email so what I will do is I'm gonna cut this out and I'm gonna put this right underneath name and then I'm gonna add a

second one for email so what happened was that we did get our error but we were not properly displaying the errors for email so let's refresh one more time John Doe and then I'm gonna hit add

customer with an empty email and there we go the email field is required we'll say John Doe John at add customer and there we go so we have John Doe in our database however we're

not displaying his email so let's change that now so back down here we are echoing out his name and then in parentheses I want to echo out May be his email and let's give this maybe a

muted color will just say class of text muted let's refresh and there we go so we have John Doe and then we have his email so we are successfully saving a name and an email for our customer

inside our database Jane Doe and that's Jane at another test com let's hit add customer and there we go so we are able to add our name and email to our customers list now one thing I

want to show you here if we say John Doe and leave our email blank and hit add customer John Doe is gone we are no longer getting that back and typically that's not what happens typically what

happens is that the name comes back in the form so the user doesn't have to fill out the entire form again imagine this form had 20-plus fields and all of a sudden because they missed one field

they have to retype the entire form that's not great user experience right so let's fix that now so laravel has a really nice function called old and so what we can do here is safe the value is

and we're going to output old and the key that we are looking for is name so old looks at the request and says hey do I have a name and if you do it goes ahead and returns a name and that's the

function that's going to help us get the functionality that we were looking for let's go ahead and also add that to our email so email let's hit save let's come back I'm just gonna pass in an email

John and I'm gonna hit add customer and there we go so now we see that the name field is required however we see that John at is still there okay let's try it the other way

another person hit add customer and there we go we still have another person up here email at email com there we go we have another person with email at email com

so now with this new old value we're bring back any fields that did pass validation and give them back to the user so they don't have to fill out the whole entire form all right one last

thing I want to fix so if we add another John Doe but then passed in a random string into email that does pass validation but that's clearly not an email so how do we

make laravel validate this as an email that's easy enough let's go back to my customers controller and so we're gonna have under the email validation rules we're gonna say it is required and it

needs to be an email it would come back let's type that in type random strings add customer and says mm-hmm the email must be a valid email address so now we are protecting ourselves from

passing in a blank field which of course says the email field is required and if we pass in a random string then we get the email must be a valid email address again revisiting back on these

validation rules if we visit the email validation rule there it is adding email verifies that the email is properly formatted awesome we're making great progress

so keep toying around with this idea maybe add a title field and maybe a phone number for our customers keep playing around with this form until you get comfortable adding more and more

fields to the database migrating the database and then adding it to the form and making sure that you put the appropriate validation rules for each of those fields and when you're ready let's

move on to the next lesson

Let’s work together to add an email field and persist it to the database.

For the best experience, follow along in our interactive school at

Course Source Code
Available Validation Rules

Hit us up on Twitter with any questions or comments @codertape (

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