Eloquent Overview & Conventions0:07
In the previous episode, we used AV Well's, eloquent methods create and all to insert data into the database and to fetch them to display them here, but we didn't talk about it more. So let's take a closer look at eloc at Eloquent and see what it has to offer. So eloquent is AL'S way of interacting with the database where we've already spoken about that. Each database table has a corresponding model,
where we've already spoken about that. Each database table has a corresponding model, which is why we created the role model which interacts with the roles table. So here you can look at all the conventions that is the table names, which we've spoken about. You can change it, you can um, give your own table name if you want, and your primary key as well. We call it id, but you can change that.
Querying and CRUD Methods0:52
and your primary key as well. We call it id, but you can change that. You can go through the documentation to see uh, what other conventions are followed and what you can change. Retrieving models, this is what we saw. We simply used the all method to get all the records from that table. But you can also use this query builder to get exactly what you want. So here they've used the flight model
to get exactly what you want. So here they've used the flight model and to get where the active column has one value and so on. So in our example, in our app, you can probably query and get only the rules with the remote location. So you can say where location remote or you can say where type is full-time and you can even order them by the time created, the date created.
and you can even order them by the time created, the date created. Or if you have some number like salary, you want to order it by highest paid job or something like that, you can even limit it for pagination. And then in the end, use the get method to actually get all those records moving further. Uh, you can get a single record in one single line. You don't have to get all the records
Uh, you can get a single record in one single line. You don't have to get all the records and then fetch one from that. You can simply use the find method to get any uh, record using its primary key. Or you can say first to get the first record and then we already saw how to insert. There are different ways to do it. Of course you can create a model instance and then say save. Or you can use the create method like we did before.
Many-to-Many Relationships2:23
Of course you can create a model instance and then say save. Or you can use the create method like we did before. You can also delete the rows using delete or you can use the destroy method. So there's a lot that you can go through in this project. We don't have much to do with uh, querying or any of that. So you can explore them on your own. But what we are going to do is explore eloquent relationships. Now in any project tables are related
to do is explore eloquent relationships. Now in any project tables are related to each other like a user model has uh, some posts maybe and posts will have a lot of comments and so on. So there are a lot of relations, like one-to-one, one to many, many to many and so on. In our app, this bookmark feature can be achieved using a relationship. Think about it. Now this is one role which can be bookmarked by many users and at the same time, this user, the one
Think about it. Now this is one role which can be bookmarked by many users and at the same time, this user, the one that is logged in right now can bookmark many roles, right? So it's a many to many relationship between the user and the role. So first we need a separate table where we can enter the user ID and the role id. So when somebody bookmarks it, we can purchase that in the database. Right now that's not happening.
Creating Bookmarks Migration3:42
that in the database. Right now that's not happening. So go back to the editor and let's create a new migration. PHP Artisan make migration. We have to create this new table. Yeah, create bookmarks table. It's created. Let me open that. And like I said, we need two columns. One is the user ID and the other is the role id. And we can add foreign keys using the foreign
One is the user ID and the other is the role id. And we can add foreign keys using the foreign ID method and we can specify the name of that uh, table. We can say user id. And the way we add the foreign key constraint is by chaining it with this constraint method. We can also chain it with the on delete method, saying that if this user record gets deleted from that table because the user deleted their account,
that if this user record gets deleted from that table because the user deleted their account, then this row also has to get deleted in the bookmarks table. Makes sense, right? And similarly, let's uh, also add the foreign ID roll ID from the roll stable. Once again add the foreign key constraint on delete cascade. Now we will run the migration PHP Amazon migrate our roll stable,
Now we will run the migration PHP Amazon migrate our roll stable, our bookmarks stable has been created. Let's take a look at it in table plus refresh. Bookmarks. Yes, we do have it. So let's insert some data manually for now. So I'll say ID one user ID one because we just have one. Uh, and let's say role ID is two created at would be now and then updated. That would be now as well. Uh, use command S.
and then updated. That would be now as well. Uh, use command S. So to actually insert this record now going back to the browser, what we actually need is this roll should show that it's bookmarked. So going back to the code, open dashboard blade view, yes, right here, this button is checking if it's bookmarked and then showing this accordingly. Now, where is this bookmark? This is a state in Alpine. And here we have just simply hard coded it to be false.
Now, where is this bookmark? This is a state in Alpine. And here we have just simply hard coded it to be false. Now instead of false, we should be able to query the database stable bookmarks and check if this authenticated user ID and this particular role is present in the table or not. And that's quite a complex query to do here, which is where eloquent relationships help us. They make it very easy if you can define the relationships. Now, how do you define a many to many relationship?
Defining Bookmark Relationships6:21
They make it very easy if you can define the relationships. Now, how do you define a many to many relationship? Let's go to the user model first. Scroll down to the bottom. Now it would be great if we could get all the roles bookmarked by a particular user. We need that for our bookmarks tab anyway. We need to show all the roles that is bookmarked by this user. So to define that method, we say public function roles.
So to define that method, we say public function roles. That is, or you could say bookmarked roles. To be more specific, we can say return. This belongs to many roles. So this user belongs to many roles. So add the roles class here and you also need to specify the name of the table as a second parameter. First of all, this is not rules.
as a second parameter. First of all, this is not rules. This is roll and it's imported. Great. Uh, you need to specify the name of the table. It does if it doesn't follow the convention. Now, laterals convention is if you're defining a pivot table, that's what we did. Bookmarks as a pivot table. If you're doing that role, user should be the name of that table.
If you're doing that role, user should be the name of that table. But this doesn't make much sense in our um, use case, right? So we say bookmarks, if your table name was role user, you didn't have to specify this. But since we are using a custom table name, we need to do this. And now let's also define the inverse relationship in the role class so that we are able to get all the users that have bookmarked that particular rule.
role class so that we are able to get all the users that have bookmarked that particular rule. You'll see why this is useful. So we say public function users. So get all the users by saying return. This belongs to many user. Once again passing the bookmarks table name as the parameter. Once we have this,
name as the parameter. Once we have this, we can check if a particular rule is bookmarked by that user. Let's create another method to make it easy is bookmarked by and pass uh, user. So we can check if this user, sorry, user, user. We can check if this user has bookmarked this role or not by returning a bullion. True or false, we can return
or not by returning a bullion. True or false, we can return first get all the users that have bookmarked. Now that we have this, uh, we've defined this relationship, we can easily get all the users that have bookmarked this role. We can say this users where user ID is equal to the this user's ID that we've passed. So dollar user ID exists.
to the this user's ID that we've passed. So dollar user ID exists. So we can check if it exists. It returns true, otherwise it returns false. Now using this method, we can come back here and we can, instead of hard coding it as false, we can use rule is bookmarked by the method we just created and pass in the authenticated user here and in. We can do that using the au function au
and pass in the authenticated user here and in. We can do that using the au function au user that gives us the authenticated or the logged in user. So once we pass in this, if this is true, then we say true else we say false. So if all goes well, we should be able to see this amber icon in the second role. Let's go back to the browser refresh. And yes, um, we have that using this defined relationship we can very easily get all
Bookmarks Controller and Views10:23
And yes, um, we have that using this defined relationship we can very easily get all the bookmarked rules by that user. So let's go back to the code and close all these files and create a new controller that is the bookmark controller. Because we not only have to display all the bookmarks later, we will also need to add the bookmark or remove bookmark as well, right? So all of that logic can be handled in the bookmark controller.
So all of that logic can be handled in the bookmark controller. Let's say PHP Artisan will use the make controller command and we will call this bookmark controller. And the type of controller that I would like this time is resource. Once you select the type as resource controller, you will see what will happen. It's asking what model should this resource controller be for?
It's asking what model should this resource controller be for? We don't want it for either user or rule. So we just select none and it's created. Let's look at what the skeleton is. So like I said, since we picked resource controller, we have all these functions index that is, it shows all the resources and create usually shows the form for creating a resource. This is for storing the resource.
for creating a resource. This is for storing the resource. This is for showing a single resource. And this is for edit. Edit form the update and then destroy. But here we wouldn't need update or edit or even show. We are not gonna show a single bookmark rule. So we're gonna delete all of that. We need destroy that is when the user removes it from bookmark.
We need destroy that is when the user removes it from bookmark. We use the destroy method. We need store to actually create the bookmark. We don't need create, we don't have a form. They simply have a button and we need index. So in the index method, I get all the bookmarks of a particular user by first using the getting the authenticated user using auth user.
by first using the getting the authenticated user using auth user. And I simply have to get rules like this, not roles, we called it bookmarked roles. Bookmarked roles. Now we are using this as a property and not a method. So we are able to easily access the property bookmarked roles because of the many to many relationships. So it's almost like this is a column in the user model.
because of the many to many relationships. So it's almost like this is a column in the user model. And now we return few bookmarks passing in the data, let's call it rules because um, that's what we are using in dashboard view as well. But the value is bookmarks. Okay? Alright, let's go to the dashboard blade view to copy all of this and paste it into the bookmarks.
blade view to copy all of this and paste it into the bookmarks. Obviously that's not a good, uh, way to do it. So what we'll do is we'll copy everything that's here inside the diff. This is a single roll, right? So let's cut it. And we're going to go into components and create a new component that is roll play pb. So this is a single roll view.
play pb. So this is a single roll view. It'll help us, it'll help everything much make it much better. So inside the dashboard, inside the four H method, we use the role component now, but we also need to pass this role as a parameter into the component that can be done using roll equals and specifying the role variable. But when you are using any PHP variable
roll equals and specifying the role variable. But when you are using any PHP variable or anything inside this value, you can't use plain HTML attributes. You'll have to add a colon there. Colon roll equals roll. Now this roll can be accessed within the blade component. So let's copy all of this, including the toast message. Later we'll change the implementation. We'll be copying the search and the for each method and the toast diff into bookmarks right
We'll be copying the search and the for each method and the toast diff into bookmarks right here, like so. Okay. Now this is not enough. We need to also change the route in the routes file. So here we are simply returning the bookmarks view, but instead we should do what we did, uh, in similar to the rule. We have to change this to get and say bookmarks bookmark
We have to change this to get and say bookmarks bookmark controller class. But not just that this is not an invoke controller, it's, it has methods. So we will have to pass in which method to call. And we are gonna call the index method in the bookmark controller. And here, let's also change the name to bookmarks index. That's the convention.
And here, let's also change the name to bookmarks index. That's the convention. And since we change the name here, we'll have to change that in the navigation bar, which is in our app layouts. So right here, this is not bookmarks anymore, it's already complaining. It's bookmarks index. Let's see if things work. Refresh. Let's go to the bookmarks tab And great, we have the single bookmarked role that is being displayed.
Refresh. Let's go to the bookmarks tab And great, we have the single bookmarked role that is being displayed. This is great. Uh, we will do more in the next episode.
