Notification in Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to another video so in this video we'll add a notification feature to this web forum so if you are here just to learn about notification in laravel that's totally fine this will be the real-life example where we'll be using the notifications so what I will do is I will add notification here and when someone replied to the threat or question created by you you'll get notified so for example if you created some threat and someone the supplied or get answer to your questions you have the notification in your dashboard so currently I'm logged in with this user so let's start so I hear a lot of got some boilerplate but it's all set up for notifications so here in our terminal we need to create our magazine first so you don't need to create our separate magazine just type PHP artisan on notifications table so this will create our migration for notification table so this will have all default fields set up so let's see what fields it will create so create notifications table just open up this migration so it will create Taif notifiable and data field so heater sowed are fishing this will create on these fields by default okay after the magazine created the sty PHP artisan migrate to clear that table in your database okay here you see this table created so after that we need to create a notification class so we got come on for that as well in order to create our notification class or this type PHP artisan make notification and name of the notification class oh are we are creating the notification for our reply to threat I'll just name it as applied to threat so that's it create inside app notification so this class will be created inside notifications alright so basically it got some boilerplate for you so like the constructor some method and action so by default it has this mail option so what it will do is it will send the notification through email so in our case our instead of sending email what we are trying to do each word are trying to just store this locally in our notifications table so what you can do is you can send email as well as here you can send you can show that in your database okay let's send it one at a time so just our database for now and you don't need this method now so mean so whatever you return here you need to make that function so if this function doesn't exist it will default to this two array so let's create our new methods I'll just make the clone of it and to database all right so this will be generally are the two added to this name to data me so now this function will handle everything so basically when we trigger our notification it will just call this method so we defined the cell which through which this notification will send data so this will are generally send the data so for example so we're creating replied to threat so what we can send is so replied time so at which time our users apply so we can use carbon now let's import this so it will send this art applied time so now we need to trigger this notification when user art applied so I will go to comment controller so add thread comment so in this function so after a user added the comment I will notify so we can notify using this user notify so actually we can access like this auth user so will notify the authenticated user and we need to pass the class which we just created so it applied to thread so this will are not fight off individual user all right so let's check if it is working till now so if you are wondering how we can use this method on user model so if you go to our user class you will see we are using this notifiable trait so this straight are will we have the access with access to this method without that trait so let's are check this if it is working so for example let's create our so let's create our reply to this thread all right so it created let's see in our notifications table yeah it created our the notification with the date sewed applied date all right so our that it's working now let's create our user interface where we'll so the notifications alright so let's go to our navbar we'll add that to nib nav bar so nav bar dot plate so if you see here to never right we'll add it here too right so at NAB bar right so if user is authenticated will execute this this code in else so we got one drop down already like this we'll just duplicate this and make for notifications so after we duplicated on this we have another drop down all right so here instead of this will the side notifications let's see how it looks alright so here we'll add a bad so instead of this class be ATP's so this is a booster class for not us hardcore this other that looks okay and here we'll add our icon so globe icon so span the plus Wickliffe icon cliff icon so globe and here yeah this looks pretty good okay in menu just remove all of these and here we'll add just the start work this for now just sick all right all right so yeah now let's play our notifications here so here will display are the so notification count instead of the start coding so we'll count these so this strap is inside count all right so yeah so this is the not be so instead are we I will display the unread notification on that okay so here we are to so that will do through the unread notifications and for each so here will display are for another notification type to see if it is working so yeah alright so on this is our working so what in our what we can do to make it more dynamic or more reusable each we load a separate the parcel view I got into these types of applied to thread so what we can do here instead will I will include our view so the name of view will be so if you want to see what this is just you just modifying the string so that we can use our view so this will basically be our view name so here let's see yeah so it created our reply to thread so the reason we created er this is we can include our view so here we'll name notification and we'll include this code there so we'll create that inside our so inside parses let's create on your folder named our notifications so notification so inside notification we will create a thread file name sip light to thread and we'll put the name here so our petal beans at layouts are parcels notification so they are parcel notification and the name of the notification alright so yeah they told us to ascending so here you can put so someone replied to your thread alright so that is the basic notifications so here we want to add the data and link to the thread where are the users applied and the name of the user would apply it okay so so as I already told also the class that is once for for passing data is deployed to thread notification class here to database so going to I was passing in deployed time so we want to pass other data like the name of user fruit light and all of those so if one of us died on this notifiable end let's see what we got by default let's create a comment yeah you can pass apply comment whatever so here we see we got the name of user are basically we got the user class so what we can do it or we can pass the thread and so basically we need to pass the to model like thread and apply model so that we can access the reply also you can access the thread let's look in the space charge so we can access the reply of thread yeah form of the space thread so so let's go to comment controller and so here so when comment created are we notify user so here we can pass this thread instance here this one so that we have access to the thread ID so that user know in which thread or someone's it light okay so now let's go to this class supplied to thread it light to touch so in constructor we can pass that set and we need to create so this thread calls to this thread and we need to create that proper property so protected set now within this strings and user so usually notifiable so yeah so yeah so basically we are we pass this thread from our Comment controller and we get it here and we just pass it in this added thread so let's check if everything is okay so now let's go back to our reply to thread our index sorry include and here I will display that our user also who created the notification so notification data so when you access this data property it will automatically convert that JSON into array okay so if you see here so it is like this in object so when you access like this data it will automatically it into Eddie so after that we access the user and the name property so this shift light or commented on your thread yeah so automatics commented on your thread instead of this threat what we can do we can this reference or we can the so the title of terms of data threat and so if the Mattocks commented on your thread color laravel eloquent so let's update this link as well and we need to pass the ID of thread so we have access to ID here so notification data threat are going to pass ID all right so let's click on this and it should take us to that threat okay so are the create our creation and view of this notification working so user can create notification and it should appear on the dashboard of user and here what we can do it so if someone clicks on this will mark these notification as thread so if someone gives this will mark these ads alright so in order to do that what I will do is winds I user click on this will send the Ajax request and we'll get that on some controller and mark or modification as read so let's go to a nap bar so I will give this ID mark as read so from top plate okay so from that Glade how we can define now we can reference to that or a Proteus so I said mmm I set yes add dot yes let's open that acet GS after so far will create min dodges instead all right so I mean that is so win someone god clicks on that function will alert clicked all right syphilis click on it okay something doesn't work okay sorry about that we need to create this inside our public so we are not using any compiler like laravel mix or web pack so we'll just ugly place this inside our J's yeah so MnDOT is nice at work you know error in console yeah okay so when user clicks on that will call a function so let's create a JavaScript function alright so let's go to nap bar and here when someone are clicks on this so on click mark notification a threat will call this function so we'll send our gal Wilson our get request to IQ I'll send the catechist to that URL and pawn all the script are that route here function okay so when I send catechist to this URL and false we will get our the authentication user and will get under notification and we'll call them mark as read okay so that's fine okay so let's taste this in action the facet open up console and keep eye on if you get any error let's see Network def now it's entered our requests so yeah we got 200 okay so now if we reverse this page we shouldn't see this notification yeah perfect so that worked so no notification yeah so this is all about notification you can add more notification or you can clear another class like this apply to class and you just need to create another view which same name so that should work so instead of 4 is you can I use four else in order to so notifications because if there is no notification it shows like this okay so what we can do instead is four else and no motive no unread notifications all right so let's of finally taste this I'll log in as another user and I will comment on the web the Mattocks post or thread and I will comment on this testing notifications so currently descending our notification to a user who created yeah will rustic that amongst login as mathematics and okay so here we got some issue when we click on this modification link it marks all notification as set so that other user also can't see that so before fixing that we need to fix another issue so if you go to comment controller so here we are notifying the authenticated user that means so if someone created the comment it will notify that user who created comment but we want to notify the owner of threat or yeah on our object we want to notify that ok so instead what we can do is we can notify the owner upset so just say thread user so we created comment as a sub tab but here notifiable ID is ID 1 which is the idea of automatic so it is perfect so this notifiable ID and notify will type so notified ID is the idea of user whom we are notifying okay so idea particularly this type user type for now okay so now the surface yeah we should see no notification here but if we logout and login as we have the Mattocks we should see our notification yeah perfect so let's fix other issues so if sub deb so if the user named subdiv will click on this notification it will mark all modification as red ok so which we don't want so so in order to fix that lets go to our JavaScript in main dot J's so we'll only mark our notification as red if there are notification related to user or if the user is capable of or authenticated to mark them as read so we are calling this function from our navbar plate and here we'll pass the notification count so let's copy this copy and paste it here so we are passing the count and in this function if count is greater than zero so this notification count so if there is notification count or if not fiction count is greater than zero or we can do not equals to zero okay so now if it is not equal to zero we'll call this mark as it yeah so no one read notification logout and login as mathematics so yeah perfect you stood there so now if we click my to it mark it as read so if you see here here slightly is usually so it is saying or thematics commented on this post but subtype or commented on this post so what we can do is are so if you go to the flight to threat dot PHP so here so user is modifiable so notify will is the user our whom we are sending notification so instead what we can do is we can pass here authenticated user so that should solve this problem okay now let's create a comment once again so comment on it and now we switch back to automatics account yeah so these times are working yeah so this is all about notification that is similar to like Facebook Twitter so we'll cover other notification in our other video so that's all for now see you next video bye
Info
Channel: WebDevMatics
Views: 76,031
Rating: undefined out of 5
Keywords: programming, web development, tutorial, php, laravel, beginners, Notification in Laravel 5.4
Id: Tkq0H-McErE
Channel Id: undefined
Length: 26min 36sec (1596 seconds)
Published: Sat May 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.