Spatie Activity Logs in Filament: A Step-by-Step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do you want to track every activity that is happening in your application is there an audit requirement it is quite possible that you building an Enterprise level application and you need to track every activity that is happening on the application and then show them in a list so that the admins are aware of what is happening on the application right and today in this video I'm going to address this specific functionality in the CRM that I'm trying to build I want the ability to track every action that is happening with most of my models for example the contacts the companies the deeds and then have a simple UI which is able to not only show me the activities across the application but also activities which has happened to individual entities for example activities of one particular deal so that it is easy for me to understand what are the different things that went through for that particular deal so without wasting much time let's get started to give you an idea of the thought process that I had I have noted down certain aspects of my requirement so I'm saying that every event inside the application should be tracked so obviously this is where um as I said I will be tracking every event and storing them into the database now because I have something in mind the first thing which logically makes sense is to see if there is an already package available in the community which can kind of do the job for me and spatty as always has something for us and today we will see how we can implement the Lal activity log to get a rather to address such requirements in your application to get started with I would first install this composer package so maybe just copy this go over here and paste so it will do the composer require it will update my composer Json the locks files and stuff like that okay and then obviously I need to publish the migration and I would also need the config so they are two different commands it is copying the table okay there are three tables it seems and the config file as well so let me add that as well all right so we have published the migrations we have published the config file why don't I do PHP Artisan migrate it says that activity log table exist and yes it would because I tried it before creating the video so I will do a clean installation and again it is creating a problem maybe something is wrong what I will do is just delete all these tables okay and I'll just run this once no need to worry about it I'll get all the data as well because the seeders are in place and with that I'm on the dashboard okay now if I refresh my database you will see that I have a new table called activity unor log and the understanding is that every activity data is going to be logged in this particular table so how does it um structure so it has an ID obviously the log name we need to understand what the log name does there's a description subject type event subject ID okay so I'm assuming that subject type will be something like the entity type uh for example if I'm updating a contact maybe the subject type will be the contact model or something like that and the ID of the subject will be here causer type obviously I am I'm here to understand that it would be the user and the users's IDS here properties some Json field I think this is um something to do with you know how uh this package basically allows us to do a before and after kind of thing I think it's in the main documentation right so calling activity changes shows me the old and the new attribute so I'm I'm assuming that's how that's why the Json data is being stored uh B uid and stuff like that so let's try and do something with it um we can go into the docs first and look at how the installation is done now I'll show you from the documentation although I know that because I would want you to understand how things are set up and what to refer okay so we can do quite a few things we will see that later but at least let's get to a situation where we can run it okay so we have done this we have migrated and these things are done um where was it I think the B basic one is logging model events this is what I initially started with and it says that the package can automatically log events such as when a model is created updated and deleted well that's exactly what we wanted right so how do we do that to make this to work all you need is let your model use this trait fair enough let's just do that then so I'll go to my contact.php and I have the tradeit called logs activity so I'll just do logs activity okay and it is complaining that that it doesn't implement the method this fair enough I'll just add that no big problem um and it expects the array to be of type log options so why don't we do that log options and so obviously we need to return log options and then defaults everything is in the documents so no need to worry and here it is log only which means we do have the ability to either do a wild card star or I can only track certain Fields now the star is quite obvious why don't we do only a few Fields so log only okay I'll just format it a bit and what do we want to log or rather what do we want to track we have contacts so the name email and phone number is definitely something which I want to track so name email phone number with that done and I think I need do the arrow and not the brackets as the next line okay this is done so what do we have I have the trade over here I have this function which is defining what I want to track and now if everything is correct what will happen is I'll just try to refresh and let me create a contact first so joho I think that's the obvious choice right joho gmail.com this is my phone number create I can see it over here now if I go to my database and look at this I can see that I have ID as one which is fine log name is default we need to understand what that log name is a bit later but uh maybe in the next video description says created okay this is fine um we have subject type and as expected this is the B uh you know the names space of the model so in here I'm saying that the current model in question or in context is the contact model event is created and the subject ID is 51 which means the contacts ID is 51 causer is type user and causer ID is One Properties well it doesn't have an old because that's got created because it got created why don't we edit it and see what is happening so I'll click over here jondo joho the 10th okay maybe something like that and then I'll just refresh and I can see there is one more entry the description says updated the model is same event is again updated subject ID Remains the Same causer type causer ID but the properties have changed and let us understand what happened so we have old this is the previous model this is the new model and I think I'll need a diff Library um to show it in a better way but obviously I can do an array diff and get only the fields which have changed and it will be kind of sufficient for me to show the exact thing that got changed or I can do a GitHub style you know before and after c of a thing I'll have to see what UI works best uh but I can understand what it is doing because I think it's the updated hook where the old value and the new value is available and it is basically storing it okay this is a good starting point I would say definitely because I am able to log literally everything about a model with just how many 1 2 3 4 5 and this one six lines of code apart from just installing the package and you know uh adding a few things here and there right the migrations so everything is set um and honestly this is a big thing if you want to have monitoring activity monitoring on your application because this will give you a very clear understanding of what is happening inside the application and the one more use case which is very interesting is something like activity log and in this video I would like to try this out show it to you before we finish this one because other things we will definitely cover in subsequent videos so we have a helper function available called activity and I can do log so let's see what happens there and I need to run it somewhere obviously and and my feeling is what I can do is inside filament I have contact UM Pages create contact I have some mutation over here right okay this is create notification mhm maybe I'll add it over here right now so activity log notification sent sorry for contact created something like that okay pretty random but I think it is enough to understand what happens okay now obviously I need to create one more contact over here joho is done so maybe jendo the third oops that's two okay uh j@ gmail.com that's fine all right and now if I hit create I should have two log entries one is basically the creation of jendo which jendo the three um Let me refresh and yes I have one event which is created and then I have a notification notification sent for contact created so whatever is in here comes as the description right and the event is null so certain things are obviously not available it has still captured the user who performed the action which is very very important I think this is a great thing because obviously if some event is happening I should be aware who created the event even if it is not directly related to any model it is important to know what happened and who created that event or who was the actor subject maybe okay um so yeah this is definitely um something which I was was really impressed with and uh it saved me a lot of time exactly what I wanted and I have few more functionalities to work on so yeah this is what I wanted to cover in this video it is very easy to use this package and keep a track of what is happening in your application yeah that's about it if you like this video then do click on the thumbs up icon and don't forget to subscribe to my channel
Info
Channel: Amitav Roy
Views: 2,306
Rating: undefined out of 5
Keywords: laravel, php, filamentphp, filament tutorial, filamentphp laravel series, filament app, app development, low-code solutions, activity logs, audit logs, spatie activity logs
Id: k8Agas9yiL4
Channel Id: undefined
Length: 14min 54sec (894 seconds)
Published: Thu Nov 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.