Laravel Tutorial For Beginners Part - 3 | Laravel Notification System | Laravel Training | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey welcome back to the laravel course this is part three and in this part we're going to be focusing on notifications so if you've ever used Twitter or Facebook or any social network you'll know that when someone follows you you get a notification you might get this by text message you might get email and you'll also see it within the within the application within the website so we're going to be focusing on that last part but the nice thing about laravel is it has notifications set up right out of the box and it allows you to have what we call what sort of drivers these are like integrations so for example we can send a notification via SMS using an API we can send a via email and we can also out-of-the-box do it via the database which lets us show it within our web application so hop over to the laravel documentation this episode's going to be more about the laravel documentation and sort of interpreting it rather than specifically notifications but notifications is the example if that makes sense so laravel has tons of sort of built in packages built-in features I couldn't possibly explain all of them in videos because they change all the time and this is so many but like notifications is one of them and they'll be very useful to us in this situation and also in almost every project he might build so that's why we're giving offering notifications but this or concept this sort of reading the documentation and interpreting it will apply it to pretty much everything whether you're doing payments or notifications or something else so head over to the documentation and look for the page notifications if you just read from the top you get introduction and creating notifications so effectively every type of notifications so user followed or user mentioned you we create a class for it and that becomes like a type of notification so we're going to do this first bit here we're going to create that notification so in our command line PHP artisan make : notification and we're going to call it new follower this is the notification that's going to be it gets sent to a user when they have a new follower so we can open up this class so just search for new follower PHP and that goes into app notifications new follower dot PHP then you might have new message or new mention that kind of thing inside of here but for now we're just going to stick with new follower now out of the box is built for emailing so you will see public function via is set to mail we can also add things to here or we can replace them so in this case we're not going to be sending emails for new follows we're just going to display them with it in the app so we're going to be using database and you'll find all the difference or drivers all the different via options in the laravel documentation you can also create your own by extending it but we're going to be going with database next we're going to remove the two mail function because we're not going to be emailing these notifications but you can see how it sort of builds those up as an example and then we've got two array so we're going to call this two database instead because we're going to be storing it in the database of course and here you got return and that is just some of the database that we get some of their data that we're going to be storing so notifications are stored in JSON which gets stored in a database table called notifications and we're going to create that table right now so head back to the documentation and in the database notification section this is what we're going to be using it gives you two commands PHP arts as a notifications table which will set up that migration remember we never edit directly to the database or ideally we always create a migration and then we migrate it so here we'll have a new migration in the database migrations create there should be a notifications one in here but has it refreshed a mine you and I just closed Visual Studio code and reopened it and you'll see a refresh I don't know why I didn't refresh automatically it normally does but we have this create notifications table migration this is pretty much we expect ID type and various other columns timestamp columns etc we don't need to edit this fortunately because it's all done for us so the next step is PHP artisan migrate I need to CD back into our project folder PHP artisan migrate and you can see it's migrated that notifications table so when I launch this in my my secret eater or client or whatever you want to call it I've got a notifications table the ID type notifiable type ID data readout created that updated at and these are just a standard out-of-the-box four columns for notifications so remember how a few minutes ago I was saying notifications get stored in JSON that gets stored in this data column right here and then the type stores obviously the type of notification so in this case new follower notifiable type very similar thing IDs and time stamps so let's go ahead and create our first notifications so in this array here this is basically the data that we would need in order to display that notification and what I mean by this is let's say for example new follower we would need to know the person who followed you so let's get the ID of the person so that we can find other information about them and then also their name and the reason we're going to do that rather than store the ID of the person and then the name is just to speed it up but you could just store the ID so in this case use our ID and then this equal to arrow all of this user ID and that's going to grab the user ID and we're also going to just store the name in there again you don't need to do this it might be better to not do this but for the sake of keeping this simple I'm just going to store the user name in here so user arrow name now you might notice if you're paying attention this user doesn't exist it would have to be in this class somewhere and we don't have a property called this user so we're going to create that so I want to set up a variable public dollar user so that's gonna be like a global variable within this class and in the construct we're going to pass in a user I'm going to go dollar this user is equal to dollar user so whenever we pass into this notification being the user account we're going to fill it in to this public variable and then we're going to be able to reference that when we set up the notification I hope this makes sense think of it like a settings table so here you're just setting what you want to store in the notification so now we're going to actually go ahead and create a notification and the way I tend to test things if you just want to write a bit of code and run it without having to actually do it properly just want to test it I just set up a get route and then call it /x for example and then give it a function so this is going to be an anonymous function inline function whatever you call it and here you can go ahead and do whatever so I'm going to echo something and in my browser I can never get over to /x I need to PHP artists and serve in order to get my web server running /x it takes you to that function right there so back in the documentation I'm going to go all the way back to the top and we've got sending notifications so in this case use the notification straight we can go user notify the type of notification and then the data we're going to pass it it's really simple and you need to make sure that you're using that notification class I'll show you that right now so in here let's go ahead and go dollar user notify and we're gonna pass in a new and we called it new follower and then in here you know you're going to pass in the user account of the person you're following so I want to go ahead and go user colon colon find or fail and I'm just going to go to so user number two we're going to be logged in as user number one and we're going to give the notification to user number one but it's gonna say that user two has followed them so that's what the user to here is for and then we need to define who dollar user is which is going to be the logged in user so auth user so hopefully if that's all don't write it down here pass error dollar user equals or fuser dollar use a notifier new new follower it looks like I haven't got enough brackets at the end there and that solves the trick so just a little syntax or other as I'll say I'm gonna get the currently logged in user and then we're going to notify the logged in user that someone else has followed them obviously this is just a test so I'm going to go ahead and run this x4 and the issue here is that we're not logged in so quarter member function notes a fire on no if you're not logged in this here is North user is not so in this case we need to login so I'm going to go ahead and stick the password in and here you go here's the recent updates as we did in the previous episode you can click on a user but that's not what we're here for we're gonna go to /x and hopefully that has just notified user number one so let's go ahead in our database refresh and we've got nothing in here so let's have a look what's going on I'll go to my new follower class and have a look here this checks out right here to database return database that'll looks okay dollar this user user that all looks okay actually turns out that my my Segal client was just stuck there is actually a notification in that so it all worked fine you'll see is created a row we have a type app notifications new follower so that's the class of the notification notifiable type being app user because we're notifying a user class that's who we assigned it to notifiable ID is one and then data this is that JSON I was talking about this is the stuff that we're actually going to read from and the reason you saw this is JSON rather than columns rather than having a user ID column and a username column it's because these could vary based on what you're notifying so for example if you're sending a notification of a new direct message you might want to store the name of the person the content of the message and attachment the time the message was sent so in a situation where the date would vary a lot you would tend to use JSON and laravel has some really nice ways of manipulating JSON in the database but we're not gonna go too much into this we'll show you a little bit but we don't want to go too much into that in this episode so now that we know the notification is definitely saving that's part one out of the way now we're just going to retrieve the notifications so we've just notified user one I'm going to comment this out so we don't run it again and now I'm going to go for each auth colon use colon code on user and the it's called notifications as dollar notification and then we're gonna DD this is display and die or dump and die something like that it just basically gives you an array it shows you the array and then dies straight afterwards so it doesn't do any code afterwards we're going to dump notification what we've effectively done here is we've got all of the notifications for the logged in user and we're gonna split them down in a for each to get just the first one so refresh that now and you'll see it here attributes there is the notification right there so if you've done sort of legacy pH whatnot legacy PHP but if you've done just plain old boring PHP vanilla PHP you might have created your own database to do this and then set up your own helper functions and all sorts of that laravel does it out of the box and this is really the power of laravel is that they thought of everything rather than you rewriting the code that someone else has already done a million times you just have it out of the box and this works for almost everyone and if it doesn't work for you specifically you can just extend levels code with your own features your own functions and you can just work with what they've got it's brilliant for that it's brilliant if you're a start-up or you just want to get your project off the ground to me it means that you can focus on the business logic and think about what your platforms gonna actually do rather than spending time on syntax or just the needless stuff the stuff that you write over and over again it's just done out of the box authentication sorted for you someone else is taking care of the security and that really is the power of laravel that's why I really like it so going back to why I originally wanted to say is we've grabbed that notification so now you can view the notifications you could display them in a nice list and then you can say dollar notification arrow and you can go mark as read I think that's the function name it's mark as read what that does is it effectively it ticks this column right here it says read out it gives a timestamp in there so that you know you've read it and then we will never show the notification again or we might show it a different color you know how Facebook will show it slightly lighter gray if you read the notification and then they'll show it like blue or something if it's a new notification that's what this column here is for my idea is tripping up my sequel is tripping over luck we can mark it as red so let's refresh the page now when I look in the database it's now red so now we won't see that as an unread notification so now if I go user unread notifications notifications and go DeeDee dollar notification and I'm gonna run this first dire so we're going to create a notification first run that and then comment this out and we're going to look for all unread notifications only unread notifications anything that we've read we don't care about refresh there's our unread notification and in the database you can see it like this but then as soon as we notification mark as read we're gonna do that refresh then we're going to Dede dollar notification refresh there's nothing there because we marked it as read and we're only selecting unread notifications I hope that made sense I went through that very quickly if not just watch it again read the documentation it should make sense because we've marked the notification as read we can't see it anymore so I think now we can move on from our test code I want to leave it here for reference but we can now actually start building this into our platform so if I go over to the main page so the home I'm gonna stick notifications I'm gonna make a sidebar I reckon with the users notifications it's not gonna be fancy just to show that it works so let's go ahead into our blade files so these are in resources views and then home and in here we've got column m d8 I'm going to add a column called column MD 4 and a record inside here with a card header notifications card body and the seleção that looks yeah we're gonna have the notifications on the side here I think that makes the most sense and it's really simple to output these because we can just go for each author colon colon user we're going to go in notifications yeah let's go to notifications as notifications this will show all unread and rate notifications we go end for each so to access the actual data for a notification we're going to be using this data column the JSON and laravel makes it really simple to you don't need to JSON the code it does it for you so let's go ahead and create a h5 tag h5 tag and we're gonna say donna notification data and then this generates an array called data user underscore name so what it's literally done is it's generated an array here based on the JSON data you don't have to do it yourself so user name started following you then underneath this we're gonna go let's say we're gonna put the timestamp so dollar notification created a gif in diff for humans I think I've shown this function before Diffie human shows you five minutes ago thirty seconds ago two hours ago CEO John Doe starts following you three minutes ago John Doe started following you eight minutes ago the reason for that is because we got it twice on her I want to go ahead and remove the second one because it's kind of unneeded you could put something nicer in here just to make it look better perhaps let's set up the link to the user account that's quite simple to do I'm gonna say put a tag in there close it off just before the closing header so paste that at the end and say /u /color notification data user ID refresh John Doe start following you click it and you go to john doe's profile awesome that's a notification set up the final thing we need to do is actually trigger the notifications when you start following someone and we need to set up the following system anyway because you don't have that yet so go to John Doe is profile and we're gonna add a follow button on his profile so over here profile I'm going to do this as a form I'm not going to do any fancy chaser Ajax here because it's not really the point of the episode let's see if we can put this in the header so form action is going to be something method it's going to be post I'll come up with the action in a minute and the input is going to be a submit start how can I have a name of follow a value of the users ID so in this case we're gonna have dollar user ID that's going to be the user that we're gonna follow I want you're going to say name is equal to user I'm going to give this a class of BTN BTN - primary and a value of follow refresh so got a follow button here and maybe we can flow that to the right this is an a from editorial as I mentioned before that will do doesn't look great but that will do because it's just a back in tutorial you might notice that we're already following John Doe and it's saying follow so we can sort that out as well so at so you can do an if statement and then stick an else on the end of it else end if so gonna say if the user is already following show this otherwise show another thing and to find out if the user is already following we can go to the user plus user messages following so I'm going to setup a new function to check if the logged in user is following a specific user so we're gonna say is following function is following dollar user yet will go dollar user so this is going to be the user that we're checking if we're following them that make sense we're gonna go return all of this following so this is going to grab everyone that we're following we're I think we called it following ID yep we're following ID is equal to dollar user ID and then we're going to count them and effectively this is going to return either 0 or 1 so it's gonna say 0 if we're not following them because there will be no row or it will say 1 if we are following them that should have made sense so function is following so if dollar or auth : current user is following and we're going to pass in the user that we're looking at the profile page right so if they are following use this otherwise use this I'm going to change the text so under follow what we actually going to say is name is equal to let's say unfollow and here we're going to say name is equal to follow so that way on the back end we'll be able to tell whether they're trying to unfollow a user or follow them it'll make it a little bit easier for us and we'll so that's a danger since they're going to unfollow refresh run page so now it's saying unfollow because we're already following them so it appears that wherever it is this is following is working so as I say this is a query so we're gonna grab a list of everyone we're following then narrow it down to where the following ID is the user we passed in count them they'll be either 0 or 1 0 always means false 1 always mean is true all good it's pretty simple so there's an if statement sorted out now I'm going to go ahead and open up our controller so the controller is called profile controller and to save time I'm going to sticky here so I'm going to a public function follow user and this is also going to handle unfollowing actually we'll call it follow or unfollow for lack of a better name and we're gonna pass in an object called request it's this here they illuminate support facades request dollar request and if you stick this at the beginning of a function if you think this is one of the variables that are passed into the function it effectively gets all the post route values to get values the user agent tons of stuff like that it brings it in in this variable you don't need it in for example the first one because we're not passing in any data we're passing user in the URL but for this one we're going to be passing something and the get there and the post values so it will be useful to have access to that and then of course we we need to set this up in the web PHP file so root colon colon get and then we're gonna say forward slash follow and the function we called it where do we call it follow or unfollow user so we're going to say profile controller follow or unfollow user and this is actually gonna be a post so my mistake we're gonna be posting the data not getting it because we're actually gonna be making changes so post and here we go where is a peripheral controller so in the request we can have a variable called request arrow follow or request arrow unfollow because that's the name of the two buttons so if dollar request follow and else unfollow so unfollow and follow so the dollar user the user that we're going to follow it is going to be user so we can grab the user object find or fail remember this will grab a user row but it will make sure it can find one if it can't find one it will error the page because it's no good to us got a request follow and this is going to be the user ID and for unfollow it's going to be exactly the same except it's going to be unfollow so in the case of both of these is quite simple for follow we're going to say auth colon colon user following and then we're going to say attach and dollar user ID so let me just reacquaint user is the logged in user of course then we get the list of people they're following and we're going to attach the user ID that we just passed in attaching just means create a new row and link that other user to me so we've got that relationship between the user and the users they're following so in this case we can attach a followed user to this users following if that makes sense I hope so it's kind of in the words here it's pretty English so a list of following we're gonna attach user and then you guessed it for unfollow it's just detach hopefully you can spell that right at erä so hopefully now that should work and we're gonna return success actually in this case we're not gonna return success because we're not doing this via Ajax so if you doing this fight Ajax you might return some JSON or you might just return the word success really it's all that it's just that 200 status code if you're not familiar 200 means success and then a 400 number would mean there was an error so you can return those status codes to the browser and then Ajax can take that status code and understand what to do with it but in this case we're going to redirect the user so return redirect and we're going to redirect back to the user that we're looking at their page so forward slash you and then concatenate dollar user ID so now hopefully fingers crossed this should work I need to set up the actual route so forward slash follow that should be I need to set this up inside of the profile blade in here in the action method is going to be post and their name unfollow and follow let's give this a go the chance is my error force erroring already request because the name is already in use in this case this means that you're using a package or using a class twice it happens quite often usually accidentally and that is in the profile controller and the reason for it is because request is already included in every single class or every single controller when I went and re-imported it I imported a different request I think it was a symphony package or something like that it doesn't really matter but you must only have one package with a name it's quite a common error that's why I'm leaving it in the video rather than cutting it out okay back to what we originally if I hit unfollow now this page has expired due to inactivity so laravel has something called CSRF tokens built in now a CSRF token it protects against cross-site scripting and what this means is let's say you are logged into your bank you are logged into Bank of Scotland your online banking great you've got it logged in you're in a different tab and then you go on to some malicious website now this malicious website is going to do a post request to your bank is going to go to Royal Bank of Scotland and it's going to hit the end point that says transfer a thousand pounds to this bank account so there'll be a URL that Royal Bank of Scotland or any bank has that hits their server and when you hit it it does a bank transfer so any site could have a form you think it's just a email signup form or something but you hit the form and then all of a sudden all your money's gone and that's kind of how it works that is a cross-site scripting attack or and this is what CSRF protection would hopefully protect against so the way this works is on the bank side in any form on the bank side any trusted form on Royal Bank of Scotland website they would have a token the only your browser knows and they know which means that when you go on some dodgy website they would need to submit that token to prove that they were a legitimate source of the post request now any dodgy website won't know the token because the token is unique to you it's usually the token will expire there's no way of the malicious site knowing what the token is and laravel has something like this built in if you didn't understand that analogy there read up on CSRF tokens and cross-site scripting and all those kind of vulnerabilities because just as a general skill it's quite important to know but it's not super important for this tutorial I just want you to understand why that error came up and why it's a good thing the way we can solve this is by in the form having CS RF field I think it's called CSRF underscore field as a function and what this will do is it will generate the HTML weather tokens let's have a look at how that looks so refresh that page and view the page source and in here let's look for the form you will see it's create an input with the name of underscore token with this long token in it and as I say no other website would know this token laravel is only ever going to accept a post request with this token and if i refresh the page to token my change note it just changes every so often so that other sites won't be able to know the token so now when i refresh this and i go ahead and unfollow sorry the page you are looking for cannot be found forward slash follow why is that we are posting to their action it's for such follow method is post how we are posting that value web dot PHP full slash follow follow or unfollow user the way I would debug this is I go into the profile controller I would stick die at the beginning of the function okay so because it went blank we know it's definitely getting to hear now this shouts out to me that this user find or fail is failing because they can't find the user so we can debug that by going Dede dollar request and we can look at everything that's getting submitted so let's have a look here in here we've got attribute not attributes it is request parameters unfollow and for some reason we're setting unfollow equal to unfollow now I can tell you why that is in peripheral blade I've actually set the value twice because I set the value and the value now this is a mistake on my end associated genuine mistake because I kind of tried to employ some dodgy coding here just to get it to work without doing some front-end stuff I set the value being the text that should show on the browser for the button but then I accidentally set the value again with the user ID so what we can do if we can do this the proper way no more time saving input type equals hidden name is equal to user and the value is equal to dollar user I D so it's going to pass in a hidden input which is going to just pass in some extra post data with the user ID and on the profile controller side we're gonna go if request follow this is still correct so if it's a follow or if it's an on follow but now we're going to go request user let's go ahead and hit that again let's remove the DD this time because we don't need it refresh sorry the page you're looking for cannot be found oh dear that's still an issue let's go ahead and deed it again to see what is good no my let's have a look at what we're posting so let's go ahead and DD dollar request this video has turned into debugging lesson which i think is actually quite good I've always said it's very useful as a viewer to know how to debug something rather than just this is how to do it here you go it's working I think this is really important so unfollow unfollow user number two okay so that's yes to me so user number two is their user and we forgot to update the unfollow a sort of route the unfollow condition to grab the right user so we were grabbing unfollow which as we said doesn't work we need to grab request user hopefully that made sense and we're going to remove the DD commented out refresh it here class app HTTP controllers or not found very common one you just need to import or illuminate supports facades off refresh that now method collection detached does not exist did I spell that wrong and in this situation here is another good lesson yet again well when I can follow it nice you're just grabbing the data I can't do anything with it it doesn't have any methods assigned to it but if you grab following as a method it will have all of their helper functions that go along with it that attach the attach all the formatting stuff but not having this just grabs the data only it's something that's where to get your head around when you first start dealing with laravel I know I struggled with it and as you can see there is a very easy mistake to make but if I go ahead and refresh now that function now exists and you can see it followed unfollowed so now I can press this and it's following me and I'm following me okay so our unfollowing and following system works if I unfollow now and go back to my homepage I no longer see mr. John Doe in there the notification is still there but that's because we're not clearing out notifications but if i go back to john doe's account you thought Such - let's follow him go back to a home and now you'll see John those really cool tweets in my timeline his messages in my timeline one thing we didn't do is we haven't set up the notifications yet the whole point of the video but so let's do that now so in this function here when we send the follow or the unfollow request we need to notify the user and we're only going to notify the more followers we're not going to notify them on the followers because people might get a little bit upset about that so let's do that now so remember back in my test I had this code here to notify a user of whatever you want we're gonna just paste that in here I forgot I pasted it in the follow section only because we're not gonna do for unfollow as is it we're gonna say user in this case it's going to be dollar user notify it's going to be the user that we're following not the user that's logged in that'll be pointless we're going to notify it the followed user that they have a new follower and the follower is author Colin : user and we need to make sure brackets in there so I'm gonna repeat myself through this just to get it over and done with so user being the other user the one we're gonna follow John in this case we're gonna notify him that he's got a new follower and the new follower is the person that's currently logged in in this case it's me and that should do the trick so now let's go ahead and unfollow him and then follow him new follower not found we need to import it same old issue again new follower simple as that refresh so now it should have notified him so now let's login as user number two and I can't remember what his login is so let's go ahead and check the users table it's test two at test comm he's got the same password so test two at Tesco calm password so you can see here he's now got a notification and this guy is gonna go ahead and follow Neil because he's a nice guy and there we go let's look in back as test at test comm that's my main user the one that's in my name Neil John Doe started following you 10 seconds ago I'm not gonna worry about clearing unread notifications and stuff that is a whole other thing you can try that on your own actually maybe I can set that it's like a little homework almost because I showed you how to do it sort of and maybe you can implement that in your own project on your own so when they view a notification have a mark as read button or something like that maybe put a bell icon and have them hired and whatever I'm gonna leave this episode here because it's dragged on quite long that is notifications for you and also very basic debugging in laravel how I debug using those dies using DD dying at certain places in the function to see where the error is being caused is so helpful and a lot of people don't do it for some reason they think it's to not advanced enough it's not taking use of all the all the debugging features that laravel has so as I said I'm gonna leave this video here there's gonna be one or two more videos left of this course and then I think we're gonna wrap it up so thank you so much for watching I hope to see you in the next one I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to any rekha channel to learn more happy learning
Info
Channel: edureka!
Views: 17,925
Rating: undefined out of 5
Keywords: yt:cc=on, laravel notification, laravel notification system, laravel notification tutorial, laravel notification 5.6, laravel notification 5.5, laravel Tutorial, Laravel Tutorial for beginners, laravel tutorial for beginners step by step, laravel project, laravel from scratch, laravel php framework tutorial, laravel framework, laravel api, laravel api tutorial, laravel api authentication, laravel beginner tutorial, laravel basic tutorial, laravel cms, laravel edureka, edureka
Id: IFz7eNQDVkA
Channel Id: undefined
Length: 36min 53sec (2213 seconds)
Published: Tue Sep 25 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.