So as I have mentioned previously, it would be best if we have a kind of an organized way to load data
into the database in the future.
It might even be autogenerated data.
For now, I think we can do with some predefined data that's easily loadable into the database.
Let's add a new command because this will be a command.
Maybe I can call that schema fixtures.
So fixtures is how you typically call this data that's predefined.
So what you can put here is either the data that your system, your application or website always needs.
Like a predefined users like super admins.
Or maybe your application is some kind of a, let's say, warehouse management.
And you just know that you're gonna have some specific data inside the database at all times.
It can be used for those cases.
Another good use case for fixtures is to load your database during automatic testing, and then you
can easily try the features of your app.
Now, our need here is to be able to click through the application to sign in as specific users, and
to have some posts so that we can see manually if the app works or not.
Now there will be quite a lot of typing here, or would be a lot of typing.
That's why I'm just gonna copy paste the data.
This data will be available to you under this video.
There is always the source code.
You can go ahead and grab it.
There is absolutely no point in typing this all.
So we start with users.
We're going to have three all with hashed passwords.
Then there will be three posts and then there will be around four different comments.
What we are going to create is just a logic for loading those.
And we will configure everything and run it.
Okay guys, now let me scroll to the top of the file.
There is a thing missing here which is the bootstrap include or require statement.
Let's grab it from this script.
So it should go at the top.
Then we can have the data defined and all the way here.
Let's grab the reference to the database from the app.
And also, let's make sure that we got a use statement for the app.
Now it's up to you.
Now it is up to us if we want to clear existing data first.
If we do want that, then we can simply run a delete query.
Now just watch out with deleting data using commands, because this can really be unsafe.
If you run this script on the wrong database, you're going to wipe out your database.
But generally, to delete data you will write delete from table name.
So we'll start with comments.
Then we'll go with the posts.
And finally we can delete the users.
If you don't want that you can always comment this out or just remove it.
It's up to you.
Just be careful with running this and just make sure you are pointing to the right database when you
run this commands.
Another thing we might want to do if we are deleting data is to reset the auto increments.
So what this means is we would like all the new records that would be inserted to start from index one.
And in SQLite you need to remove something from a special SQLite table called SQLite sequence.
Let me quickly jump to the database.
That's this table.
That's special table that is internal for SQLite.
And inside this table it refers to all the tables in the database.
And it is just saying what's the next number for autoincrement integers that are used for primary keys
typically.
So that's optional.
That's actually completely optional.
But if you would like to do it then we would go with delete from SQLite sequence.
We only want to delete some specific sequences.
So the word query and we check if the name that's the column is in the specific set of values, which
is users posts.
And can you guess that.
Yes that's comments.
Okay.
So we are deleting the data from all three tables.
Then we are resetting the autoincrement sequence numbers.
And then next up we should just go over every user and insert it into the database.
For that we can use models.
So we can go with user.
Create user.
And as you guessed that we are going to do it for every single database that we have.
So we are going to use the post model to create posts.
Just make sure you don't mix up those variable names.
Finally we've got this comments array and we are going to insert some comments.
Now it's up to you if you'd like to report the success after every single database table that's populated
or not, we can just finish off with saying that fixtures Ships loaded successfully.
Dot.
Newline.
Semicolon.
That's everything.
If anything would go wrong, then we are just getting an exception which is already handled in the command
line.
So we don't have to worry about this at all.
The last thing that we should do is oh, one more thing.
Let's also add this to Composer.json scripts so we can run that easier.
This can be maybe schema fixtures.
And the script name is Schema fixtures.
All right.
Now we should be able to run composer schema fixtures.
Everything was loaded.
Let's close all of those windows and jump to the database.
You might have to refresh if nothing's showing up, but I can already see that there are some comments.
I've got some blog posts with automatically populated dates, and we've got three users with different
roles, so we'll be able to use that.
Definitely.
Now let's run it again to see what happens.
So everything should be removed and just recreate it.
And also everything should start from ID one.
And this is what we wanted.
Okay.
So now that we have some data we can go ahead and really implement some logic displaying blog posts.
The admin dashboard and all the UI parts of our blog application.