Laravel 5.8 – From The Ground Up
Lesson 10 of 52 · Laravel 5.8 Tutorial From Scratch – e10 – Adding Email For Customers · Education
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 example.com 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 example.com 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 example.com 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 https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Available Validation Rules
https://laravel.com/docs/master/validation#available-validation-rules
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!