hey welcome back to my channel and in this video we are going to talk about relationships with filament so honestly speaking what good is an app if there are no relationships with a model you know where let's say you have you are saving something and that particular record depends on another model a classic example in our case is a student is related to a standard right that's what we are trying to do so in this video we will see how we can Define that relationship and then filament will automatically handle the saving of that relationship
of a student being associated with a standard so let's get started to start with what I will do is go into my database migrations and in the students table I'll have to add a column for an id4 and mention the model which we are going to reference this will be standard all right once this is done I need to update the relationships of these models and see whether everything is correct or not in terms of Association and stuff like that so A student in here if I do standard so it a student belongs is to
standard okay and I'll just update a few things over here for example I'll generate the dog block and the return type okay and I'll add the other relationship as well which is a standard has many students so return this has many students okay again I'll just add these dog blocks and the return type so that the code is a little more clear on what it is doing now one more thing which we need to add is the column that we just added which is standard ID because we will be populating it so it should be
fillable and with these two things in place why don't we clear this out and handle our Factory first so inside the standard Factory student Factory rather I'll need to add a new column which is standard ID and what I'm going to do is for now just keep it at one because any new student let's just assume that you know he is in standard one and instead of complicating things because we can send the attribute while creating a factory if you want a custom standard okay so that's how things are let me go to my terminal
do a refresh of the database and see if things are proper they are all the students are in standard one which is fine now put this in place I think the first thing I would like to do is see if I can get the information in the listening table so what should we do we'll go to student resource find the table and in here ideally we should be able to do standard let's see once we refresh okay login route is not defined let me fix this problem this is happening because you know I refresh the
database and the tokens and stuff doesn't exist so it is you know complaining and I don't have an authentic login route and the authentication middleware is expecting that the route list will be filament auth login fair enough so filament auth login is the URL I'll try to access this and I am redirected to the login screen now let me log in so I click sign in and I see some Json data okay I guess what is happening is it got the entire Json data the relationship so fair enough I'll just do name and we are
done fair so all this is clear now let's see if we can get that in the edit form so go into a form there is a component called select so this is like this um this will be the name of the field which will be saved so I'll do standard ID like this and this is kind of required and I'll do relationship name of the relationship is standard and the second one is what title column name so it will be name you just format the code and we are here now I see the standard one
being clicked why don't we change it to standard nine hit save right this is not the correct format so DPS one two three four five six maybe this one is fine I hit save changes okay it's still not um accepting it so maybe this save changes and it says it's saved why don't we go into the listing page and see if Mrs this person is one standard nine so this is working definitely working and let's see if the create is working as well so I have name student ID and just select standard 5 create and
yes everything is working properly we can go into the database and check to do a complete hygiene check and yes everything is fine so as you can see but this is really a very simple way of doing things one more thing which I would like to show you is an interesting thing which is if I come over here and just add a parameter called searchable I have this a new search box and let's just try to search for amitav and we get it let's just say we want to search for standard five it is not
available right now but if I add that same attribute over here and hit refresh STD 5. I have one record I do nine I have one record so can you imagine this kind of a basic search functionality could have taken a long time but for us it took just two attributes right so yeah that's about it uh that's what I wanted to cover in this video which is you know how do we show relationships save them while creating an entity if you have any questions or suggestions drop a comment below if you like this video
do click on the thumbs up icon and don't forget to subscribe to my channel