Laravel Authentication & Mailing Crash Course | Laravel 8 Mailing | How To Mail In Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up laravel developers is dari here and i hope that you're having a great day now that we just finished the laravel api course i thought it might be good to dive into authentication and mailing in laravel this is a pretty big step forward if you think about the projects that you can build in laravel if we take an e-commerce as an example you offer products to the customer and let's say that they can also subscribe to your newsletter if they want to order a product they need to be authenticated since you need their personal information and if you want to send them a notification or an email of their order you obviously need mailing now before i continue on with the video i want to quickly let you know that you can support a channel through patreon which is linked in the description down below you get pretty cool benefits such as a private discord group where everyone is helping each other out and you can decide the next video series that i'm going to make for the polls so if you're interested to join the link is down in the description when i created my larval for beginners series which is also linked in the description down below i added a chapter at the end of the course about authentication and authorization not knowing that you could make an entire course about it i want to cut all the small talk and introduction videos and how to set up a project and i actually want to dive right into the code now i have a folder on my desktop where i store my laravel projects called workspace but i also created a shortcut command called cdw which will change directories to the workspace so let's hit enter now we're inside my workspace as you can see right here and i obviously need a new larval project so that's right now larval new art app let's hit enter and this might take a second so pause the video and i'll see you back in a second the folder has been created so let's change directories inside our project so cd art app hit enter inside our auto app we need to add larval's ui through composer so let's perform composer require larval forward slash ui space double dash dev hit enter alright this was pretty fast for this course we obviously need to pull in the authentication scaffolding so let's perform php artisan ui view space double dash art hit enter and it's asking us to run npm install npm run dev so let's copy it paste it hit enter and this will probably run into an error of laravel mix let's perform npm audit fix double dash force and now let's run npm run watch and as you can see it's compiling as well so we just fixed that now before i dive into my project i want to show you which versions i'm using for this project as you can see we run mpmron watch so we need to open a new tab i can do that by pressing ctrl or command t let me zoom in cdw again cd into art app and in here let's perform php artisan space double dash version and as you can see i'm using laravel's framework of 8.38.0 to be honest the versions from 8.0 until this one won't make a big difference the issue comes into play if you're using older versions such as larval 5 or 6. now to also check out our npm version let's write down npm space-v and as you can see the npm version is 6.14.11 whether you get experience in a different programming language than php or laravel you probably know that creating an application with login and register sessions password resets remember me access permissions can be well pretty time consuming and one of the reasons i started using laravel is honestly to save myself some time when working on projects so right now it's finally time to dive into our code editor so let's open visual studio code and let's open our workspace as well i need to drag down my visual studio code alright so in our workspace let's drag odd app into the code editor let's close off the folder because we don't need it let's make it bigger and let's also zoom in let's close off the welcome screen now using a framework like laravel can also have its downsides and yes it does and it's even coming for me since everything is so easy to go and the documentation on larval is very good most people completely forget how the flow works and that's what i want to show you in this video every larval project comes with its own users table migration and user model which you probably have seen when you migrated your fresh project for the first time so let me show that to you if we open database there is a folder called migrations and let me make this a little bit bigger you can see that we have three migrations to create users table create password resets and create file jobs now the one that we need is the first one so to create users table let me make it smaller again and intellisense updated to well let's say dismiss all right let me scroll down right here you can see that a user has a unique id name email and the email verified add which is a timestamp there is string so for the password the remember token and the timestamps now whenever you migrate this file it will be used with the user model so let's open that one right now let's open app models user.php looking at this file you usually won't touch it a lot you can see that the namespace is obviously app forward slash models it has some use statements as you can see right here it has a class name it has two use statements inside the class which i will cover in a bit and it has two properties the first one is called fillable and the second one is called hidden whenever you're using the fillable property you're basically telling the application well whenever you want to create a new user at least the name email and the password needs to be filled in and for the password and remember token exclude them because they don't need to be shown to the user and honestly that's right isn't it why would you want to get one of those values and print them out for a developer there's actually not a legitimate reason for it probably to see if your password is encrypted but that could also be done through my sequel there's a property called casts which will cast the email verified at to the date time i skip the most important words right here well actually the most important words so let's go at the top and as you can see our class user extends the authenticatable so i'm on a mac and i have php intellisense installed which allows me to click through on a word the way you do that is by clicking on alt and clicking on the word that you want to click through now in our case it's authenticatable so let's do that then here you can see the entire pad so it's in the vendor directory folder called laravel and so on until the file user.php you can see that the class extends the model which is the default model and it also implements three other files the first one is the authenticatable contract and this is something which is called a contract as the name implies this will require methods that will then allow the framework to authenticate instances of this model to the odd system we have the authorizable contract and if you have used laravel before you know that there's a can method which will allow the framework to authorize instances of this model or their access permissions in different contracts the last one is the can reset password contract which will basically allow you to reset password of any entity whenever you pull in the authentication scaffolding you also pull in some controllers so let's go at the top of our page open http controllers the art folder now to show you this example or the controllers we need to go back to the terminal and in here perform php artisan serve and the reason why i'm doing this is so i can show you the files that we're working with now as you can see it generated an url for us so let's copy it let's open a new tab for the browser let's paste it in and let me zoom in a little bit all right now let's hop back to the cli one more time because i want to show you all the routes that we got and in order to do that we need to open a new tab again i need to zoom in and let's go to the right folder all right and in here let's perform php artisan route column list now let me zoom out for this one whenever you pull in the authentication scaffolding you will see a total of 14 routes right here and almost all of them are related to the authentication scaffolding now a cool thing is that the authentication scaffolding will automatically pull in a home controller so let me show that to you right here it has a home controller in the controllers folder and as you can see inside the uri we have a home page which will go to the home controller and it searches for a method called index besides that we have a login screen show the login form and something that might be weird for you is that we basically have two login forms so the get head and the post the get method is basically to show the page and the post method is to submit the form we have a logout page the password confirm the password email reset and the register page now one thing that you might have seen is that all these files that you can see right here go through the art folder so every single page right here is using the art folder and this is actually the reason why i showed you the routes since that's the first place we need to look so let's go back to visual studio code in here you can see that we have one two three four five six controllers that are related to authentication i want to go over these controllers in a logical order so whenever you start a new application you need to register first so let's open that one so the register controller what i want to do is to actually register an account and show you the different things that happen inside the controller but before we do that we need to set up our database so let's open our emv file let me close off the vendor i need to change my host to localhost the port is alright the database is alright and i am using a password of dory1234 save it and close off the file let's open the register controller again let's go to my sql now what we actually could do is to open a new tab and write down well zoom in write that in my sql we need to create a new database so let's perform create database art app and hit enter and you can see that one row has been added now let's go back to the previous tab that we had in here we need to run php artisan migrate and let me zoom in as well right now we are ready to continue on with our controller i won't be going over the use statements namespaces and class because it's pretty straightforward it's something that you already need to know but the one thing that i skipped inside the model was the use statements now these look like a new statement but they are actually called different these use statements inside a controller class are called traits and this trade will be called when the controller is being called now if we scroll down you can see that we have a property right here called redirected to and this is actually pretty important to showcase what this does i want to go to the browser click on register inside the top right corner create a new account so let's say code with dory password is info password is something secret alright if we click on register right here you can see that the register process went pretty fine but we have been redirected to forward slash home and this is being done inside the redirected to property of the controller as you can see it's calling the route service provider colin colin home what is common colon home if we press on alt and click on route service provider scroll down you can basically see a constant here called home and this is going to an endpoint of forward slash home and if we double check it inside the browser you can see that our endpoint is forward slash home obviously we can easily create a new endpoint so let's do that let's go right below our constant home create a public constant test and let's set it equal to forward slash test save it go back to our register controller and let's replace home with the content that we have called test let's save it let's go back to the browser let's click on our name log out and let's register one more time name is test password is test add code with diary let's add a password click on register and as you can see it redirected us to forward slash test and the status code is a 404 because the page has not been found now let's go back to visual studio code let's change it back to column colon home and let's continue on as you can see our controller is using a constructor so whenever a user tries to hit the register controller it will look if a person is logged in or not through the middleware there is a validator and a create method the validator method will basically define how to validate registrations so if you like to change some rules say that you want to change the password minimum characters to let's say 12 you can easily change it right here now the last method is the create method which will define how to create a new user based on the incoming registration which is the variable data whenever you register you will log in automatically just like i showed you a minute ago but let's see how the login controller works so let's open it if we scroll down you can see that the login controller is using one trait called authenticates users which will bring in the redirected users it also has its own property of redirect 2 which is heading the home constant but you can change this one as well now there's one method inside the login controller and that's the constructor so whenever the login controller is being called it looks if the guest already exists so inside the session and then it will show you the log out button now i can go over the confirm password for got password reset password and verification controller but if you scroll through it let's just open one you must have a clear understanding of what is going on right here because it's pretty much the same it's using crates it has a redirect and it has a constructor that might go through the middleware now the next step is the routes so if we scroll down and open the web.php file inside the routes scroll down you can see that three routes are defined but i actually want to focus on the odd column column routes now what this basically does is showing all the endpoints that i just showed you in the cli you can obviously define all the routes by yourself but think about it if you have a resource for quad applications why would there be a special type of route for all the different views that you have in the controller it's so easy to just define a resource or a route now that i've talked about the controllers model routes and migrations i think it's time to take it a step further by talking about the authentication scaffolding since views are a very big part of it it will take care of the routes inside the views of the resources folder there's a folder called views and in here you can see a art folder all these files and the layouts folder as well got automatically created when we run the php artisan make aud command and all these files have a bootstrap-based layout and all of the form fields are already correct now if we open the login blade file we obviously have a form where we can log in and i won't go over all of the stuff that we have right here but i want to quickly show you something which is pretty important if we scroll down to the bottom you can see that we have a label and the input right here and this is basically the remember me check box that we have inside our view let me show that to you change the endpoints to forward slash login oh we're logged in let's log out let's click on login and as you can see right here we have a remember me checkbox now this is pretty much the place where we process the remember me checkbox that you can see in the ui this remember me function does not work out of the box since you need a long life access token inside your database so let's go to the cli let's go to the my sql tab and let's use art app now let's desk the table users and as you can see right here we have a remember token column which can be known now if we select everything from the table users let me zoom out all right you can see that the remember token off code with dowry and test are actually both null and this is happening because i've never clicked on the input field when you usually log in you log in without a remember token but whenever you click on the remember me checkbox a boolean will be passed as a true to the auth attempt method so where can we find this method let's go back to visual studio code let's go to the login controller and let's click through on authenticate user now if we scroll down you can see that we have a method or somewhere right here called attempt login you can see that we are going to check if a remember input field has been filled and this will return a boolean now if we go back to the browser let's log in with info at darin ozar let's click on the remember me checkbox click on login you can see that we are indeed logged in and if we go back to the database select everything from users again you can see that a long life token has been created for code with dory if we close off the browser open it one more time let me make it full screen and let's go to the right endpoint you can see that we are indeed logged in because we cannot see the login and register button now the next thing that i want to talk about is the odd middleware and i actually have talked about it before but not really in that a lot of places in our application let's open visual studio code and let's go to the login controller let's go to the constructor all right a lot of places in our application we have been using the middleware as you can see it accepts a string called guest now how does this work if you want to perform checks to see if a user is logged in or not you will usually do that through the middleware now larval uses a route middleware where some very important rounds are defined and these can be found inside the app folder http and there's a file called kernel.php if we scroll down to the bottom you can see that we have a property called routemiddleware and if you look at the names if you look at the names you can see a couple files that are related to authentication the first one is obviously odd and this is used to restrict route access to authenticated users we have the art.basic we have the guest right here which will restrict access to unauthenticated users and the last one is the can right above and this is used for authorizing user access to given routes just like any other application we pretty much concluded that it allows a user to authorize in your application you can basically have control of what they are allowed to do in larval you can use gates to determine if a user is authorized to perform a given action so instead of adding a lot of codes and see through if statements if a user has been set you can use gates now the odd service provider is located inside the app folder you can find a folder called providers right here and in here you can see a file called alt service provider so let's open it we need the boot method now whenever you do something inside the boot method you're basically calling methods on the odds vacate and before we dive into our gate we need to add a new column to our table users because we need to check if a user is an admin or not now we obviously don't want to roll back to lose data so if we go to the cli go to the other tab and perform a php artisan make me a migration called add is admin underscore to underscore users space double dash table is users so we're basically creating a new migration because we want to add a new column to the table users hit enter all right it's creating the migration let's go back to visual studio code database folder and it's obviously the last one and as you can see right here we have a new table while another new table a new migration for the table users in here we could basically say table create me a boolean called is admin and the default value is zero now let's save it and close it off hop back to the cli perform php artisan migrate and as you can see the add is admin to user has been migrated now if you go to my sql let's perform select everything from the table users let me zoom out you can see that this admin has been added right there but it's actually pretty small for you before we define our gate we need to have a new page to see if a user is an admin or not so let's define the route first let's go to our home controller right below our index method let's create a public function private method let's return a view of private save it let's create a view so inside our views folder create a new file called private.blade.php and let's create an h1 in here and let's say private screen if is admin has been set to true save it close it off and the last thing that we need to do is to define a route so let's go to the web.php file at the top let's pull in our home controller so use app backslash http backslash controllers backslash home controller save it let's go down let's create a new route under the welcome route say route call and call and get to forward slash private let's pass in a second param of the controller some home controller column column class comma single quotes and search for the method called private save it let's go back to the browser to test it out let's change the endpoint to forward slash private there was a typo alright as you can see private screen if this admin has been set to true has been printed out right now we are finally ready to talk about our gate so let's go back to the odd service provider at the top let's open it and inside the boot method we need to define our first gate so let's say gate column colon define so we're going to define a new gate inside the parentheses of the define method we need to add some params now the first param is the key and this needs to be a string that makes sense to what you're going to do we're going to check if the admin is set and then the admin can only enter that specific page so in single quotes let's call it admin dash only let's add a comma because we have a second param since we need to define the closure so let's say function parentheses curly braces and let's hit enter inside the function that we just created we need to pass in a param and it's going to be the object we're going to check for access so it will be variable user so given the object that we got variable user we can check whether a user is a admin or not so right inside the brigade we could basically perform a if statement where we're going to check if the user is admin is equal to 1. now like i said and i'll repeat myself once again we defined a boolean in our database so we're going to get back one or zero you can obviously write the logic however you want but at the moment let's keep it like this now the closure of our return value needs to be a boolean now if the user is a admin we want to return true since the one that we're checking is true outside the if statement we want to return false because it's not true if we save it and go back to the browser and refresh it you can see that it still works since we still need to do something inside our controller we need to tell our controller to listen to the gate so let's do that let's go to our home controller we need to pull in our gate first so let's say use illuminate backslash support backslash vacates backslash gate save it let's go down to our private method because right here we need to check if the gate is allowed so let's perform an if statement and inside the if statement we're going to check if gate column colon allows which is a method parentheses allows method accepts two params the first one is the gate name that we have defined in our odd service provider so admin only or let's copy it paste it in single quotes now the second param is the actual logged in user and like you probably know you can grab that with aud parentheses access operator user if a user is allowed let's return a view of private now make sure that you remove the second return value if a gate is not allowed we need to return something as well and what i want to return is abort 403 if we save it and refresh the page you can see that we're being hit with a forward tree and this is happening because we haven't set the is admin inside our database so let's change that let's go to item let's say update users set is admin equal to one where id is equal to one so codewordari has id number one hit enter let me zoom out let's select everything from users again and as you can see the is admin boolean has been set to one now let's go back to the browser let's refresh the page and as you can see we are allowed to see this page next to the allow method that i just showed you inside our controller we could also perform a denies method so let's replace it we will check if the gate the nice admin dash only save it go back to the browser refresh it and well we have been hit with a 403 because it's not being denied but it's allowing us the denies method is pretty much the same as saying gate call and colin allows but adding an explanation mark right in front of it so it's not true save it refresh the page and it's still a 403 this example was pretty simple but this should open a lot of opportunities for you guys if you want to create an application with multiple roles what i recommend is creating something like an enum that will interact with different rules like admin creator user and so on one of the most important ones in my opinion is mailing in laravel in web-based applications sending an email or notification is a very very common requirement and to be honest this can be done in multiple ways there are loads of male functionality layers that you can build on top of the default layer functionality which is the swift mailer in order to see your mail configuration you need to go to the convict folder and in here you can see a file called mail.php so let's open it if you want to send actual emails to clients or customers you need something which is called a mail driver and if we scroll down you can find all the drivers that come with largo for mailing we have smtp ses mail gun postmark send mail lock and array now one thing that all of these drivers have in common is that a lot of variables are being called from the emv file since laravel 5.3 our new mailable syntax has been developed before this ever came out there was a classic mailing system which was very easy to use but for the purpose of this video and to not make it longer than it should be i'm going to focus on the mailable now the goal is to create a specific php class that will represent a mill and this needs to be generated in the cli so let's go to the cli now before i show you how we could actually generate a mail let's write down php artisan help make colon mail you can see the usage of the mail function so whenever you try to perform a make column mail command you need to pass in one argument which is the name so the name of the class that you want to create you can see that there are a couple options you probably won't need most of them except for the markdown but we will cover it in a bit what we want to do is to create a new mailable class so let's say php artisan make call in the mail called welcome mail if we hit enter you can see that the mail has been created successfully and if we go back to visual studio code you can see that a new file has been created or a new folder called mail so let's open it and it's right inside of the app folder so let's open welcomemail.php if we look at the class right you can probably see a class that looks familiar to you that imports two traits the first one is queueable and that's used in order to queue your email and the second one is serializes model and this is used so that every eloquent model will be serialized correctly you could also find the constructor which is empty right now so let's skip forward next to the constructor there is one method defined which is the build method now inside this build method you are going to define the view that you want to use as you can see right here you're basically returning a view so this view it searches for a folder called view with a file name of name now besides the view you could also pass in the subject or anything else that you want to show the user if you have properties that you want to use inside your mailable class you need to pass them inside your controller so they will be available inside the template that you're going to create we won't be doing that in this video but i will do it later on there's one thing that is not correct in this file it returns a view of view.name but we haven't created that file now in order to create the view ourself we could also do that in the cli so what we need to do right now is to remove welcome mail so let's delete it move to trash go back to item hit the arrow up and we're indeed going to create a welcome mail but i want to add a dash m flag to it as you can see in the options menu dash m stands for markdown and it will create a new markdown template for the mailable what i usually prefer to do is to create a folder called emails so let's say space emails dot and the dot stands for the folder like i said before inside the emails folder i want to create the file so let's call it welcome let's hit enter and you can see that email has been created successfully as well so let's go to visual studio code we indeed have our welcome mill open it and as you can see in the return markdown you can see that emails.welcome has been called we still haven't created the file but like i said if we go to resources views you can see a new folder called emails which will refer to emails in the markdown and if we open it there is a file called welcome.blade.php which is basically the welcome after the dot let's open it because this is not the typical blade.php file that we're used to if we want to open it in the browser we need to create a new route because we obviously want to change some stuff up so let's open web.php at the bottom let's create a new row so let me add a comment route for mailing so what i want to do is to basically do the same thing as always so route column column get inside the get method we have a first param which is the endpoint so forward slash email i want to pass in a second param so let's say comma which is a function curly braces hit enter what we need to do inside our route is basically to return a new instance of the welcome mail class that we just created we're getting an error message because our web.php file does not know where welcome mail comes from so what we could do is to remove it say welcome mail and you can see that the use statement will be automatically pulled in if we click on the option so let's do that let's add parentheses ourself and a semicolon and if you go to the top you can see a use statement of app backslash mail backslash welcome mail if we save it it should actually work in google chrome so let's go there let's change the endpoints to forward slash email and as you can see the predefined interface is generated for us and we can see it in the browser since we created it with the dash m flag now the most important question is probably where is this all coming from because if we go back to the welcome.blade.php file we're seeing stuff on the screen that we haven't seen before as you can see we're using a hashtag introduction if we go to chrome it's this piece of text right here so let's change it up let's change it to welcome to my authentication and mailing course save it go back to the browser refresh it and we just changed that piece of text what about the paragraph so what about the body of your message so let's change that up to you i would like to thank you for watching this video save it google chrome refresh it and it has been printed out as well there's also a button right here which does not look like a clickable button but it's using a component to mail column column button which we will cover in a second but it does accept an url so what we could do is to basically say https column column backslash backslash www www.codewithdari.com change the text to visit site save it go back to chrome refresh it and we have a new button right here now what about larval right here you usually don't want to send the email with larval in it if we go back to visual studio code you can see that it's calling a convict method and is looking for a app.name now this is searching for the name inside the emv file so let's open it up at the top of our screen you can see app underscore name which is equal to larval so let's change it to odd app save it go back to the browser and larvo has been replaced with autoapp next to the template that we just created we can also extend other templates use sections parse variables contain conditional statements so pretty much anything that you can do in a normal blades view what i want to do right now is to send an actual email we can either set up a mailing provider or a local development in this video i want to focus on a mailing provider called mailtrap let's open a new tab in the browser and let's go to mail trip dot io let's hit enter whenever you want to test your emails before you send it to an actual client you can use mailtrap what it does is basically catching your email in a virtual box so you can text and optimize your email so what i want you to do is to pause the video sign up it's very easy you can use your google account github account or office account or you can just sign in with an email pause the video set up an account and i will see you back in a second whenever you're done with the saddle part you will probably land on the screen that i have on right now inside my smtp settings we have a section called integrations right here as you can see it's on c url so if we open the drop down you can see a lot of programming languages obviously the one that we need is laravel 7 so let's click on it and as you can see the box right here has been changed and our smtp data have been printed out and if you're familiar to the emv file you can see that this looks pretty much like that emv variables so what we could do is to copy it and before i continue on don't copy or use mine because it obviously will be different for you so let's go back to visual studio code and let's go right below our mailing let's actually replace it let's hit backspace paste it right here save it whenever you want this to work you need to basically break off your php artisan serve and run it again so let's do that let's go to i term let's go to the php artisan serve tab if you want to break off your serving you need to press ctrl c and as you can see we just broke it off hit the arrow up run php artist and serve again our development server has been started the application that we got knows alright we have a mailing provider called mailtrap as they can see right here so the variable mill underscore mailer and it knows how to interact with it with all the different variables before we make it work we need to define where we're going to mail it to let's go to the web.php file again right inside of our route we need to add one more line of code so let's go right above our return instance let's say mail let's hit enter because we need to pull in the illuminate supports vacate mail colin collin then we need to define the receiver so let's say 2 parentheses semicolon inside the 2 method we need to basically pass in a string and this string will be the email of the receiver so let's say single quotes info at darinozar.com this can be any email that you want to use the best way to do this is to have the actual user that is logged in but let's undo that for now outside of the two method we need to add an access operator because we need to define one more method which is where we want to send it to so let's say send and inside the send method we need to pass in the new welcome mail save it once again be aware that you pull in the illuminate mail otherwise you will receive an error now let's save this file let's go back to google chrome go to our localhost refresh the page and as you can see it takes a bit longer so something is going on right there let's go back to mailtrap and as you can see we have received our first email this was a very simple example during the next few videos i want to show you how you could reset passwords add attachments and it will also show you how you could verify your email before you register now this was it for this video in the next video i want to set up a local development for mailing if you do enjoy my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit the subscribe button in the last video i showed you how you could easily set up miltrap for mailing and in this video i want to show you how you can set up mailing for your local development because well some of you might not want to set up mailtrap or use the internet to test mailing a very cool tool is larval's log driver which will lock all the emails that you try to send to your local larval file before i show you how you could set it up i want to show you where the log file is located inside the root of our application we need to search for a folder called storage and usually it's stored at the bottom right here and as you can see there's a folder called logs in here so let's open it and you can see a larval.log file let's open it the content inside the log file depends on whatever you have done inside your application if you have a freshly installed application you probably won't have a log file because you haven't run into errors or done something with it just like setting up your mailing to mailtrap or any other driver you need to set your mailing to the log file and this can be done in two different ways but i will show you the most easiest one if we open the emv file you can see that our mail underscore mailer variable is set to smtp if we change it to log save it go back to the browser now in the last video we set our mailing in a way that every time the forward slash email endpoint got called an email will be sent to a specific email that's hit enter all right let's go back to visual studio code let's open our larval log file let's scroll to the bottom and as you can see right here you can see that our mail has been printed out but it also includes the html part at the top but you can also see a section where you just see plain text there are multiple ways how you can do this but whenever i want to add an attachment to an email i usually don't do it inside the web.php file like we did before when we defined our route right here but i use a controller in between so let's do that as well let's navigate to the cli and then here we need to perform a command to create a new controller so let's say php artisan make me a controller called emails controller let's hit enter our controller has been created and since we're already here let's move on and create an email class as well so let's say php artisan make me a mail called attachment mail i also want to add a markdown so a dash m flag and let's also define the file path so let's say emails dot attachments so inside a folder called emails and create a file called attachment.blade.php alright let's hit enter our mail class has been created successfully and right now we need to define the route for our mail because we need to see what we're going to send inside the browser first so let's go back to the web.php file and let's actually comment out the route that we had now right below the route let's create a new one so let's say route column column get we're going to add an endpoint to single quotes forward slash email we also want to pass in a second param so comma and we're going to pass in an array of the emails controller let's hit enter to pull in the entire class at the top right here let's add two columns give it a class comma and the second param is the method that we want to call so let's say email before we could open it inside the browser we obviously need to create the email method inside the emails controller so let's open our emails controller and let's get rid of the comment and let's say public function email and what we're going to do inside the method is basically defining the logic that we want to send in the email but before we actually define our email let's actually return first a new attachment mill parentheses let's pull the class in let's save it let's go back to the browser let's change the endpoint to forward slash email and as you can see our email has been printed out let's take it a step further let's go back to the controller let's get rid of our return value now we're ready to define the logic so what we want to do is to say mail let's pull in the class column colon we want to send it to someone so the two method now this method accepts one param which is the string of the email that's going to receive it so let's say info at darinozar.com now let's attach a new method to it called send and what we want to send is basically this view that we have so let's say new attachment mail now let's save it if we refresh the page inside the browser right now in order to add an attachment to your mail you need to do that inside your mail class so let's open attachment mail inside the build method you can see that the attachment markdown has been defined let's add a second method so let's hit enter and on the line below let's say subject let's say that the subject is how to attach images the subject is fine but it's still not why you clicked on the video because you are interested in how you can upload a file now before i started this recording i added an image inside my public folder where i created an img folder and an image called test.jpg just to save us some time with the recording so pause the video and come back when you have done that as well so whenever you want to add an attachment you need to add a new method so let's hit enter and on the line below let's say an access operator and we're going to call the attach method now the attach method accepts one param that needs to be filled in let's actually do that before i show you the optional param we need to define the path to our image first so let's say public underscore path which is a method and inside a method let's say single quotes forward slash img because the public path will look inside the public folder forward slash search for a file or image called test.jpg let's save it now what i need to do really quick is to go to my.emv file because in the last video i changed it to log so let's set it back to smtp save it let's go to the browser let's refresh our forward slash email endpoint as you can see it takes a second so what we could do is to open our wrap and we already received an image with a subject of how to attach images let's click on it and as you can see in the top right corner it accepts one attachment let's click on it and it's test.jpg now like i said there is a second parameter we can pass in which is an array so let's do that let's go to visual studio code to the attachment and right after the public path let's add a comma let's add brackets and let's hit enter in here you can define things like your mime type or change the name of your image because you don't want to send an image or a pdf with the name test so what we could do is to say as let's say test attachment dot jpeg let's add a comma and let's define the mime which is equal to application for slash jpg now if we save it and go back to google chrome refresh our localhost one more time go back to mailtrap you can see that we have received a new email if we click on it look at the attachment you can see that we changed the name of the file in web applications a very important topic is notifications if we imagine an app where we are going to notify students that they are allowed to enroll for a new course we should look at it as a relational way one notification will be sent to many students so we're going to create a one-to-many notification channel just like with sending emails where you perform an artisan command to pull in a php mail class you can do the same exact thing for notifications so let's navigate to the cli and here we need to create our first notification class so let's say php artisan make me something called a notification it accepts one argument which will be the name of your class just like anything else encoding keep it related to the task that you're going to create so let's call it test enrollment if we hit enter a notification folder will be created so if we go to visual studio code you can see that inside our app folder there's a notifications folder with one file a php class called test enrollment so let's open it if we take a look at the class we can see a lot of similarities with the mail class that we created in the last video but there are a few things different right there inside the constructor we're going to pass in relevant information that you want to send through the notification if we scroll down you can see a vaio method which accepts one param called notifiable and what this allows us to do is basically define in which notification channels we're going to use there's a two mail method right here and this is an individual method that is being used for every notification channel that allows us to specifically define how to send one of these notifications next to the fact that you can send out notifications you're also able to save them inside a database we're not going to cover that in this video but i will show you how you could easily generate a migration that is predefined for you let's navigate to the cli and in here let's perform php artisan notifications colon table this command will generate a migration let's hit enter let's go to visual studio code let's open the last migration and we have a new schema called notifications now if we close it off and go back to our notification class so our test enrollment we're able to set everything up right now and we can focus on the right and controller after in order to pass in data inside the constructor we need to create a new property so let's do that let's say private variable enrollment data this private property that we have will receive data and it will be passed inside the constructor so let's do that let's say variable enrollment data inside the constructor we could get rid of the comments and say that this enrollment data is equal to the variable that has been passed in all right let's scroll down because inside the two mail method we're going to change up some things since we're receiving data through the constructor now let's get rid of everything that's in the line method let's say that we want to print out this enrollment data brackets body now let's also get rid of the string inside of the action now let's say that we want to print out this enrollment data bracket single quotes enrollment text we could also replace the url method right here so let's do that let's say this enrollment data and we want to pass in the url now as a thank you we could keep it static but let's change that up as well let's say that this enrollment data and we want to search for a thank you and this is pretty much everything that we need the next step is to set up our route and controller so let's do that let's save the file we are going to pass in data through the controller so let's go to the ci and let's perform a php artisan make me a controller called tests enrollment controller let's hit enter our controller has been created so let's go back to visual studio code and open it now our test enrollment controller has been created but before we do anything let's define our route first so let's open our web.php file right below all the routes that we have let me actually get rid of the odd error because the class hasn't been pulled in alright right at the bottom let's say that we want to create a new route with a get method we want the endpoint to be four slash send dash test enrollment we're also going to pass in a second param because we just created a controller that we want to pass in so let's add brackets because we're going to pass in two params the first one is the class name so we just created tests enrollment controller column column class comma single quotes because we're going to pass in the method so let's say that the method that we're going to create in a second is called send test notification save it close it off and we're ready to focus on the core logic inside our controller so let's open our test enrollment controller let's get rid of the comments let's create a public function send test notification inside the notification class we define a property called enrollment data that will contain specific data that we want to send to a user so let's redefine it right here so let's say variable enrollment data is equal to an array let's go inside the array and hit enter what i usually do is to finding the keys first since we did that already in the notification class it must be easy for us right now first one is the body the second one is the enrollment text then we got the url and the last one was the thank you we are ready to find the values of our keys so for the body let's say you received a new test notification for the enrollment text let's say you are allowed to enroll now for the url let's just keep it equal to the url method that will redirect to the forward slash endpoint and as a thank you let's say you have 14 days to enroll before i continue on with notifying students on their new test enrollment i want to go over two different ways of sending a notification since you can either use the notification vacate or use the notifiable trade to an eloquent class which sends a notification to all users now let's start off by doing that let's start off with a notifiable trait by default every module that you create so let's open the user model you will see a use statement at the top right here which will pull in a notifiable trait that will be used inside the class this allows us to do something with a user inside the controller so what we need to do is to pull in the user inside the controller so right above our array let's say variable user is equal to user let's pull in the user model comma colon first so give me the first value right below the array we need to send the notification via the trade and since we're using the notifiable inside the user model like i just explained we're going to use the two mail method right below our array let's say that variable user is notifying what we're going to notify is a new test enrollment which is the notifications class that we defined then here we're going to pass in variable enrollment data which will be used inside the constructor of the class save it in order to send the mail we need to go to the browser obviously let's go to larval let's change the endpoint to forward slash send test enrollment hit enter we have not defined a front end so the endpoint will be blank for us but i and you probably too have set up mailtrap to catch emails so if we open mailtrap you can see that we receive one email called test enrollment which is a notification for the user besides the notifiable trait there's another way to send emails and that's through the notification vacate to be honest i prefer to use a trade but i will show you the notification vacate since it is something you will see a lot on the internet so let's go back to visual studio code let's comment out this line right here and on the line below we're going to use the notification vacate so let's say notification let's pull it in and be aware that you pull in the support vacate colin collin sent the first param is where we're going to send it to we grabbed up the first user so let's say variable user comma and then we're going to pass in the notification class so new test enrollment and in here we're going to pass in variable enrollment data now be aware that you pull in the right use statement the one that goes to illuminate support vacates and then to notification let's go back to the browser let's refresh our localhost forward send test enrollment go to mailtrap and we just received a new email like i said both of them have advantages and disadvantages with the method that we just used we basically need to pass in the notifiable and the notification right here which is a little bit extra work but a huge advantage might be the fact that you could pass in more than one notifiable at the same time you've probably run into web apps that send you a sms notification whenever you try to log in or think about the delivery company in your country that sends an sms notification whenever they are near you to drop off a product that you have ordered there's nothing built into larval that does that for you but you could use a package called nexmo and before i continue on this video is based on the documentation of larval and the package that we're going to use called nexmo you can easily pull in next mode through composer but you do need an account later on in order to work with their package since you need their api credentials for now let's navigate to the cli and perform the composer require larval forward slash nexmo dash notification dash channel command let's hit enter and as you can see the next mode notification channel has been pulled in maximo is an api based application that will connect your application directly to any carrier around the world with nexmo you can do way more than just sending sms messages think about automated facebook notifications voice messages verify transactions or implement a tool factory authentication if i could give you my honest opinion it's very important to get a good understanding of nexmo since you will definitely run into it later on in your career the next step is to publish your vendor directory so let's say php artisan vendor column publish now you can see a couple options right here of what you want to publish now on my screen number 10 is nexmo so let's say number 10 hit enter so we have copied the nexmo.php file from the vendor and we have placed it in forward slash convict for slash next mode so if we open our nexmo file let's go to convict you can see a file that has been added right here called nextmode.php and right here you can see api key and api secret this will make sure that it looks inside the emv file to find our secret key and name that we're going to add if it does not exist it would print out nothing so let's set up our nexmo account let's go to google chrome let's open a new tab and go to google right here let's write down next mode and we need the second link or the first one next mode to developers.next mode in the varnish api developer section you will see a lot of different notifications that you could use with nexmo sms messages voice messages programmable videos and way more so what we're going to do is to click on start building for free and what you need to do is to create an account as you can see a pretty cool thing is that you don't need credit card requirements so it's free for everyone once you log in you will get an email that you need to verify before you continue on so pause the video set everything up and i'll see you back on the dashboard if everything went right for you you'll be sent to the nexmo dashboard as you could see on my screen right now under the getting started tab you can see an api key and an api secret which we do need in a second what we need to do first is to well we could actually copy our api key go to visual studio code open the emv file at the bottom alright let's create a new variable at the bottom of our page called nexmo underscore key and be aware that the name needs to be equal to the name that we're using in our nextmode.php file right here close it let's set it equal to the variable that we just copied now let's create next mode underscore secret i'm not going to show you my api secret key copy it paste it right there and i'll be back in a second i've closed off my emv file so you can't see it anymore now the next step is to create a controller since we need to have a new endpoint that will send an sms whenever that endpoint will be hit whenever you log in somewhere you'll be redirected to a new endpoint with a piece of text on your screen which says that you need to verify your account first so let's navigate to the cli we need to create a new controller for our sms messages so let's perform a php artisan make media controller called sms controller hit enter our controller has been created we're going to get there in a second to write our logic but for now we need to define our route first so let's go back to visual studio code let's open the web.php file and let's say that we want to route command column get our first param is the endpoint of the url so single quotes for sms comma brackets we're going to pass in the controller that we just created so let's say sms controller column column class comma method is called index right now we are ready to work on the logic of our sms message we haven't defined a method insider sms controller let's close off our web.php file let's open the sms controller create a public function index and in here we need to define a couple things first we're going to pull in the nexmo class so let's write down nexmo let's pull it in in the use statement call colin message and we're not going to do anything inside the message method but we're going to create a new method called send the send method accepts an array so let's add brackets and hit enter and be aware that you pull in the next mode larval vacate nexmo now in here we need to make sure that we pass in three values now the first one is called 2 which will be the phone number that will receive the sms so let's say receiver comma the second one is from so the sender comma and the last one is text now this will be the text inside the sms so let's say test sms just to prevent some issues myself i won't be adding my phone number since i actually want to keep that private right after our next small message we could basically say echo out a message sent i will add a screen recording on the screen right now where you see that we received an sms message now one thing to keep in mind is the country code so in my country it's usually plus three 6 where my number starts with just get rid of the plus and that will do the work for you and the laravel videos that i created in the past and especially the project that i've made we touched on the authentication scaffolding a lot with the double dash art command that we performed at the beginning of this course we basically pulled in an entire frontend scaffolding but there are a lot of things that you need or can adjust in order to make it a working login and registration system on a lot of web apps on the internet you usually don't get access to the data right after registration since there's always a verification attached to it now the email verification does not work right off the bat since you need to make some adjustments to make it work now if we navigate to our controllers you can see an art folder right here so let's open it with a verification controller this controller handles email verification for users that create a new account inside the user model that we have we need to implement the verification class you can see that the user class extends authenticatable and we also need to say that it implements the must verify email save it we're not done yet but we are done inside a model close it off the next step is telling the route that the user needs to be verified before they can proceed so let's open the web.php file right here you can see that we have the odd column column routes in order to tell the routes that verification needs to be enabled we need to pass an array inside a method verify which is an array and let's say we want to set that equal to true so we're enabling verification save it there's one more step left right here whenever you do anything related to authentication you pull in the middleware in your controller let's open it let's open the home controller let's open it you can see that this middleware is using the art let's remove it let's pass in an array instead because we need to pass in two values the first one is odd again comma and the second one is verified this is all you need to do in order to add verification now the last thing that we need to do is to test it out i'm using mailtrap to catch my emails so i can navigate to the register page i already have it open now the name is dari my email is info darianasar.com my password is something secret and my confirmed password is also something secret equal to my password click on register it's taking a second all right as you can see we're being hit with a screen that we haven't seen before because we need to verify our email let's open mailtrap we have a new email and right here you can see that we have an email with a button called verify email address and before we click on the button you can see a uri right here which has a verified token inside of it so let's copy the link let's replace it with mailtrap and as you can see we have verified our account and we are logged in this was it for this video where we verified our email after registration if you do like my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit the subscribe button you
Info
Channel: Code With Dary
Views: 4,191
Rating: undefined out of 5
Keywords: laravel 8 user authentication, authentication redirects, laraval 8 authentication, multi auth in laravel 8, laravel 8 authentication tutorial, send email laravel, laravel 8 mailing tutorial, complete laravel tutorial, laravel development tutorial, laravel get auth user, laravel mailing tutorial, laravel 8, laravel 8 tutorial for beginners, laravel tutorial youtube, laravel full course, complete laravel 8 course, learn laravel 8, laravel, learn laravel step by step
Id: 4L79-JJxVjM
Channel Id: undefined
Length: 70min 6sec (4206 seconds)
Published: Mon May 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.