Playing
01. Generate Fake Data with PHP using Faker

Episodes

0%
Your progress
  • Total: 14m
  • Played: 0m
  • Remaining: 14m
Join or sign in to track your progress

Transcript

00:00
If you've ever needed to generate fake data for your application, whether that's just static content on a page, or you want to add fake data to your database for testing, you know how time-consuming it can be.
00:14
And in fact, it is just a waste of time. In this video, we're going to look at a library called Faker, which basically generates fake data. And this is random, really easy to get going with. And we'll look at an example of seeding a database table called Articles with some fake article data.
00:34
Now this makes it really, really easy because when you're in a development environment, you're going to be generating things like pagination for records. And to create enough records to work with things effectively, it's just going to waste too much of your time. So we'll look more about Faker in just a moment. Let's first create the Articles table that we want to seed.
00:58
And then we'll look at how we do this. We'll download Faker as a dependency for our project. And we'll get going with it. So let's create this table quickly.
01:08
It's going to be called Articles. And SQL Pro has generated an ID for me. It's unsigned primary key and auto-increment, obviously an integer. We're going to add some more fields like title, body, created.
01:25
And we're going to be generating 100 records here, basically with a refresh of a page. And this is going to be random enough that you can work with the data. And Faker also allows you to generate tons of other information, URLs, email addresses, first names, company names. There's so much that you can do.
01:45
OK, so let's just modify these. We're going to set the title as Varchar 200. The body is going to be text. And the created at will probably be a date time.
01:56
So we've got our fields ready. We're not going to enter any records into our database manually. We're going to let Faker handle this for us. So there's a couple of ways that you can install Faker.
02:07
You can either download the zip from the GitHub page and include it in your project and use Faker's autoloader. I wouldn't recommend this necessarily because using Composer as a dependency manager is so much easier. So let's do this using Composer. If you want to do it using the downloaded zip, that's absolutely fine.
02:31
The concept of using Faker is exactly the same. So I happen to have a terminal window open. I'm currently inside the Faker directory, which I have up here. And I'm going to use Composer to search for this package.
02:44
So make sure you have Composer installed. And then we're going to run the Composer search command. And we're going to search for Faker. It's going to go ahead and look this up in Packagist.
02:56
And here is the library that we want. So what we can now do is go ahead and show more information about this to determine which version that we want to download. So this shows the versions here. We don't want to pull in the master.
03:10
We don't want to pull in perhaps this one. We do want to pull in something like 1.4.star. So inside of my text editor then, I'm going to create a new file. I've already got index.php created, just a blank file.
03:25
Create a new file in here. And this is going to be my Composer JSON file. And we're going to be pulling this in as a development requirement, not just a requirement. The reason for this is it's unlikely that in a production environment we'd want to use this to seed a database.
03:45
You might need to, in which case you can happily include it as a normal requirement. In this case, it's just going to be on my local computer. So let's then paste this in. And remember we said we wanted 1.4.0 or you could do 1.4.star, but in this case we use 1.4.0.
04:06
And we need to go ahead and install these dependencies. And there we go. So Faker has now downloaded. We have a vendor folder created for us with the library in it.
04:22
And we also have the Composer autoload file so we can easily just include all of our dependencies in our project. So let's get going then and start using Faker and see what it's capable of. I'll obviously need to require in the Composer autoloader. If you've downloaded it from the zip, just go ahead and include it as per the instructions.
04:48
So we need to instantiate the factory class, which is part of the Faker namespace. So I'm just going to create a variable called Faker. And we instantiate factory under Faker. And we actually have a static create method here.
05:04
But that's all we need to do. We don't need to configure anything. We don't need to set anything up. We can now just start looping and using all of the properties and methods that are available within this factory here to go ahead and within the Faker library to actually just generate what we need.
05:23
So inside of the GitHub page, the documentation is pretty extensive. You can see here that we've got stuff like lorem ipsum text so we can generate words, sentences, just a single sentence. And you'll notice that the type of these outputs vary. So, for example, if we wanted to generate a sentence, we can choose the amount of words.
05:47
If we want to generate sentences, we can also choose the amount of words. But in this case, sorry, in this case, we generate an amount of sentences if we were to use something like paragraphs. So this is a method that allows us to define how many paragraphs we want to generate. So say per article we want to generate 20 paragraphs.
06:11
We'd use the paragraphs method, provide 20 as an argument, overriding the default 3. This would return us an array which we can then manipulate and insert into the database. That's exactly what we're going to be doing. We can also generate things like first names for male and female, address information, phone numbers, company information.
06:32
We can generate date times. Date time is quite interesting because then you can use that, further manipulate it. Internet things, so like URLs, email addresses, usernames, passwords. The list goes on and on. Take a look at this and see. And to be honest, probably most of it will be relevant to your data needs.
06:52
So let's start with a really simple example and generate some words here. So what we need is a loop. So within this loop, Faker is going to keep up to date with how many times we've looped. We won't go into that, but it will then generate fake data based on the amount of times that that property or method is accessed.
07:14
So, for example, let's do a for each loop and let's just stick the range function in here and say we want to generate 1 to 100. This will generate an array with 100 elements and we can just loop that as X. So this will basically loop 100 times. You can generate, say, or use a for loop if you prefer.
07:36
It's entirely up to you. So let's just take a look at an example. So I'm going to echo out Faker as we instantiated up the top there and I'm going to say word and then I'm going to just generate or append on rather a break. So we now get the following a list of 100 random words.
07:58
There we go. Simple as that. But we want to do a little bit better than that. And we actually want to seed our database with data and the concept applies to any data you're working with. So after you finish watching the video, you can go ahead and apply the same concept to any table, any data.
08:13
So the first thing I want to generate then is a body for my article because this is the more complex item that we're going to be doing because we need a little bit of manipulation. And for this, we're going to be using Faker paragraphs and we're going to generate how many paragraphs or tell this method how many paragraphs we want. So in this case then, if we just do a print on the body, this is going to give us the following. So we've got an array of 20 items full of paragraphs.
08:44
So we want to do something a little bit different here because we don't want to just plunk loads of an array arrays into a database table. What we want to do is we want to surround each of these paragraphs with a paragraph tag. So how do we do that? Well, we can use the implode function, which will take an array and it will convert it to a string and join it by the string that you give it within this method.
09:06
In this case, I'm going to join it by an ending paragraph and the starting paragraph. And you might be thinking, well, why is that? Well, in this case, if I go ahead and append on a paragraph here and then end on an ending paragraph here, what this is going to do is it's going to join it by a starting paragraph and an ending paragraph, then a starting paragraph, then another ending paragraph. So it will basically wrap whatever's output from this for each key in a paragraph tag.
09:38
And we can check that by just echoing body. And you'll notice that because we're doing this within the browser, it will be rendered as HTML. If we view the page source, you can see here that we've got a starting paragraph. If we follow this all the way down, we've got an ending paragraph and so on and so forth.
09:54
So this is now data that's ready to go into our database. OK, so let's before we go on, just create a connection to our database. I'm just going to be using PDO here. And obviously, within here, we're going to choose the driver.
10:10
We're going to choose the host, so localhost or 127.0.0.1. We're going to choose the database name, which is site for me, and the credentials for login. For me, it's Homestead and secret. So let's go ahead and perform a query here.
10:30
Actually, before we do that, we want to make sure that we remove all data from that database table before we seed it. The reason for this is if you run a seeder, which is essentially what we're creating in a very simple form, we want to remove everything we've already created and then recreate it. Or we're just going to keep appending more and more to our database.
10:51
So we'll say DB query. And in here, let's say delete from articles, simple. So that's going to remove all the articles, then populate the database. So in here, then, we can create a simple query.
11:08
We don't need to worry about SQL injection. It's all very rough. We are querying within a loop, but we're not concerned with the performance on a development environment. It doesn't matter if you have to wait a couple of seconds.
11:21
And we're going to do a simple query just to insert. So insert into articles. We choose a title, a body created, much like we created here. So title body created.
11:35
And we then specify the values. In this case, then, we have a string here for the title. And all I want to do here for the title is say Faker sentence. And we'll say we want 20 words in that sentence.
11:55
So you can adjust the word count. The next is the body. We already know that we have generated that surrounded by paragraphs. Now we want to go ahead and insert the date time.
12:08
And if we go ahead over to the Faker library, search for date time, we have a couple of options here. We can use Unix timestamp. What I'm going to be using is using an ISO 8601 format, which is going to generate something like this.
12:24
And that's going to insert a random date into that record. So this is really useful for things like sorting records by date if you have that kind of functionality. And you can also set a maximum value as well. In this case, the maximum value is up till now, up to the present moment.
12:43
So let's go ahead then and say Faker ISO 8601. Let's just surround that like so. And there we go. We should be good to go now.
12:58
So we've generated the body. We've output a sentence here and then a date as well. So running this then is going to clear the table of any records that are currently there. And then it's going to go ahead and loop through and populate that with data.
13:15
So as mentioned, we have a title here of 20 words. We have obviously our incrementing IDs. We have a body here, which you can see is just lots of text surrounded by paragraphs. We've already seen this working.
13:28
And then we have the created at date, which within our application we might want to go and sort. So that's pretty much it. We've used Faker in a very basic example to see how we can populate a database on development environment just to test and play around with data if we don't have any real data to hand.
13:50
And like I said, there's so much you can do with this. So check out the documentation. See what you can generate. And this is going to save you a lot of time.
1 episode 14 mins

Overview

Speed up development and use Faker to generate fake data, like words, paragraphs, names, email addresses, and so much more! Here, we look at seeding a database with 100 records of data at the click of a button.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!