FREELessons: 12Length: 2.1 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.5 CUD: Create, Update, and Delete

We've read from the database, but you also need to know how to create, update, and delete records in the database. I'll show you how to do that in this lesson.

3.5 CUD: Create, Update, and Delete

If you are writing a database driven application, then chances are very good that you're going to manipulate that data. Because if you're not, then well, you need to question whether or not you need a database. Now, whenever I say the term manipulate, I'm meaning more than just updating records in the database, because that is manipulation. You're updating the values in those columns. However, manipulation is much more than that. It's creating new records. It's deleting records. It's basically doing everything that's, well, change the state of the database itself. And that is what we are going to talk about in this lesson. And we're going to do essentially what we did a few lessons ago, where we talk about the more complex queries, we're going to create a new file, called create-update-delete, and we're going to write our code, and then execute them in Tynker. So the first thing we're going to talk about, is creating. And we've already seen two ways of creating records within the database. If we go to the database folder, and then seeds, let's look at the manufacturer's, and the guitars seeder classes. Now, I purposefully left the manufacturer's seeder this way, so that we could refer back to it. We use DB table. And then, call the insert method. And then, we pass in the data that we want to insert, and that's it! We have created a new row. So we're done there. Whenever we look at the guitars seeder class, we used our model class. It has a method called create, and then we passed in the information that we wanted, to essentially insert into the database. But instead of saying insert, we are creating. Now, this is important, because we actually have two terms here. We have a new, and then we have a create. Whenever you create something in eloquent, you are creating on the database. So we have created a new record, where the name is Vela. The year is 2017. We've created another record, where the name is Starla, and the year is 2015, and I want to make that distinction, because here in a few moments, we're going to see those two terms used, and while on the surface, you might think that they're doing the same thing they're actually aren't. So we've seen how to use the create method. However, we're going to revisit this, because there's a slight little issue here. So let's just put that at the back of our mind for now. We want to create a new guitar, so let's do so. And we're going to call it, talman. And we're going to new up a new instance of our model class. So we're going to say App\Guitar, and we're going to call that constructor, so we are creating a new object, and we're going to build it one property at a time. So this guitar is called talman, so we'll set the name to talman, and then the year is going to be, I think, 1996. I don't remember if that's what the year is of my talman or not, but at this point in time, all we've done is create a new object. This is something that we would use within our application, and we haven't done anything else, so it doesn't exist in the database yet. In order to make it exist in the database, we have to save it. So every eloquent model class. It's going to have this save method, so that you can save the object that you are working with. If it is a fresh, new model object, that has its properties populated, then it's going to create a new record in the database. So it's important to note that until you call save, you have not actually saved that record in the database. You have to call save, and that will create that record. So let's take this code. Let's go to our command line, and paste it in. And whenever I press enter for the save, then we see that that is true. So we now have a guitar, called talman, in the database. But the talman was made by a company called, Ibanez, and we don't have that manufacturer. So let's do that, and we're going to do so using our model class. So it's going to be very similar to the create method, actually, it's going to be exactly like that. But there's going to be a slight problem that we run into. So we're going to say App\Manufacturer, and create. And I believe that there's only one column here, and that was name. So we're going to say name is equal to, or not equal to, but name is the key, the value is Ibanez, and that's it, that will create a new record. Now, we are not creating a new model object here. We are creating a record in the database. Remember, the term create, you create within the database itself. So if we take this, and we go and try to execute this, we get an exception. MassAssignmentException, so what is this MassAssignment thing after all? Well, it's an issue. It's actually a developer cost issue, because developers didn't take the time to properly validate the input coming from the user. The very first rule that you need to learn about user input, is to not trust it, validate it, make sure that the data coming into your application is the data that you expect, and throw out everything else. Developers were not doing that, and it's not necessarily Laravel developers. This actually started with Ruby on Rails, but developers that make ORMs, like eloquent, have built in protection, so that users can't hurt themselves, basically. So we essentially have to say that, okay eliguents, we want the name property to be mass assignable. And we do that inside of our model class. So let's go to our manufacturer class, we have a protected variable, called fillable. This is an array, and each item in the array, is the name of the property that is okay to mass assign. So in our case, the name property is okay, that's perfectly fine. But if we had, something that was really, really sensitive, like a flag, to indicate that a user is an administrator or not. Then that is something that we would definitely not want to put inside of the fillable array, instead we would want to put it inside of another protected variable, called guarded, and that could be is_admin. This of course doesn't exist, but this is the idea, so that you have fillable, and anything inside of the fillable array, is okay to mass assign. Anything that is inside of the guarded array, is not okay to mass assign. But since we don't have that is admin, I'm just going to comment that out. So now that we have set the name to be fillable, we should be able to execute that line of code, but first of all, we need to restart tinker, so php artisan tinker, and then we will run that create command. And there it has created that manufacturer. Now, notice that it did two things. First of all, it created that record. So it has the name Ibanez, it has the created and updated fields. And then it has the ID, but it also returned that information in the form of our manufacturer model. So we could then use that within out application, however, we needed it to. Well, we have a few methods that we need to talk about. The first, is called find or new. So let's create a guitar variable. And we're going to say App\Guitar, and then the method is find or new, now this is just an extension of the find method, where we pass in the value of a primary key, and if a record with that primary key exists, then it is returned to the guitar variable. Otherwise, it is just a new model object. So in this particular case, we get an actual guitar, because we have a guitar with an ID of one. However, if we pass in 100, well, we don't have a guitar with an ID of 100, so this returns just a new model object. So notice the term new here, whenever you see new used in this context, you can think of it as newing up a new model object, just like you would do in code itself. So if we actually inspect this, let's copy this, and let's paste it into tinker. We run it, and we see that we get a guitar object. But there's no data associated with it, because we don't have a guitar with that ID. And if we look at the properties, name is going to be empty, and year is going to be null. So there's no data associated with it, whatsoever. And if we wanted to check to see if we had some actual data, what we could do, is use the exists property. If it exists, then it is in the database. Otherwise, it's just a new model object. So yo don't have to check the individual properties, or anything like that. You just have to use this exists property. If it's true, you have one in the database. So let's look at another method. It is somewhat similar, and it's called FirstOrNew, and this is just an extension of the first method. We pass in the properties that we want to try to match. So if we wanted to try to find a guitar with a name of Custom 24, then we would pass in an array, that has the name as the key, and then Custom 24 as the value. And of course, we don't have one of these in the database, so this is also going to return a new model object. But then, there is a method called first or create. So we'll say App\Guitar, FirstOrCreate. Now, we're using the term create here, so this isn't going to just new up the model constructor. This is going to create a new record in the database, and return that to our guitar3 variable. But in this case, we need to pass an array that contains all of the properties or the columns, that we want to check for, and assign, if it doesn't exists. So if we did want to create a guitar with the name Custom 24, then we would do this, and then we would say year equals, there not equals, but the value for year is going to be 2014. So it's going to attempt to find a record with this data. If it finds it, then it returns it, otherwise, it's going to create a new record and the database, that contains this information. So that is the create method. Now, what about updating? Well, we can update a record in a couple of different ways. The first, is to do so by setting the properties that we want to update, and then calling the save method. We kinda did that a few lessons ago, whenever we modified the name of one of our guitars to flying v. But if we wanted to just use a single method, so that we can do that all on one line, well that would like this. We have updateOrCreate. Now, this is a little bit different than just plain ordinary updating, because if it doesn't exist, then it's going to create a new record with this information, so if we try to update a record with an id of one, which we do, then we need to supply the information there. So there's two arguments that we pass to this method, the first is an array which contain the pieces of information that we want to attempt to find in the database, in order to match a record. The second, are the values of the columns that we want to update. So in this case, if we have a guitar with an id of one, let's update it, so that its name is no longer just vela. But it is velasatin, because it is a satin finished as opposed to a high gloss finish, however, if we attempt to find a guitar with an ID of 100, well that doesn't exist, so it's going to create a new record with the name that we have specified here in the second array, and we will have not only a new model object, but a matching record in the database, as well. To finish this session, we're just going to look at normal deleting. I originally wanted to get into soft deletes, but that requires us to modify our models, and in the next lesson, we're going to be doing just that. So it kind of makes sense to just hold off and include that in the next lesson. So in this lesson, we're just going to talk about a normal delete, that is actually removing a record from the database. So this is done on the model object itself. S the first thing we need to do, is you'll have a model, so we need to find a guitar, and let's just pick an ID of 15, because we know we have a guitar with that ID. And then, we simply say delete, and that will delete that guitar. So if we run this in Tinker, then that's going to remove the guitar with an id of 15. So if we try to retrieve that, let's say findOrNew, then we're going to see just a normal guitar object, that is completed devoid of any information. So that is creating, updating, and deleting records, using eloquent. In the next lesson, we are going to start talking about relationships. And as such, we are going to completely just blow away all of our migrations, and almost start from scratch. So we'll get started with that in the next lesson.

Back to the top