Sometimes you are testing project in localhost, you will also need data to do proper testing. For that, you will need to import database from the running site or need to insert data manually. This is not the proper way of making testing. Or you can generate fake data programmatically. This is where Faker is needed.

Faker is a PHP package that generates dummy data for testing. With Faker you can generate mass amount of testing data as you needed. Faker comes preinstalled in Laravel framework. You can also use Faker in other frameworks or your own native PHP websites.

In this article, we will create Laravel project and generate dummy data for Article. So let's start.

Project setup

First of all generate latest Laravel project with bellow command in your Terminal or CMD.

composer create-project laravel/laravel faker

This will create project in the existing directory. Import the project in your text editor and setup your database in .env file in the root of your Laravel project.

As we have earlier said, Faker comes preinstalled in Laravel, so we don't need to install Faker package in Laravel.

Now let's create database migration using belllow artisan command:

php artisan make:migration create_articles_table

Now add table fields in the migration class in the up() method.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title', 255);
            $table->integer('user_id');
            $table->string('slug', 255);
            $table->string('keywords', 255);
            $table->string('description', 255);
            $table->text('content');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

Run the migrate command to generate table in the database:

php artisan migrate

We will also need to generate Model class. So create model using command.

php artisan make:model Article

Generating Factory class

That was our project setup. Now let's start to work on generating dummy data. For that, first create bellow Laravel artisan command:

php artisan make:factory ArticleFactory --model=Article

It will generate factory class at database/factories/ArticleFactory.php file. Open that file and add fields in the 'return' array as bellow.

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Article;
use Faker\Generator as Faker;

$factory->define(Article::class, function (Faker $faker) {
    return [
        'title' => $faker->text,
        'user_id' => factory(App\User::class),
        'slug' => $faker->slug,
        'keywords' => $faker->text,
        'description' => $faker->text,
        'content' => $faker->paragraph,
    ];
});

Check this official documentation for the property of $faker object to generate data according to your table fields.

Now open database/seeds/DatabaseSeeder.php file and add the bellow lines in the run() function. This will call factory class and will generate dummy data in the database tables.

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        factory(App\User::class, 50)->create();
        factory(App\Article::class, 200)->create();
    }
}

This will generate 50 records for the users table and 200 records for the articles table.

Factory class for users table is already generated in Laravel bydefault at database/factories/UserFactory.php. You may want to modify fields according to your users table.

Conclusion

In this way, you can generate dummy data in your localhost website and make proper testing. For more latest articles, follow us on Twitter and like our Facebook page. If you have any query or give your suggesting in the bellow comment section.


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

3 Comments

  • Image
    Parbriz
    February 21, 2020 Reply

    &lt;p&gt;Vand parbriz Bucuresti, geam luneta, geam lateral&lt;br /&gt;Servicii calificate de inlocuire a oricarui geam auto la domiciliu &lt;br /&gt;- 2 ani garantie -&lt;br /&gt;Parbriz,Parbrize,Parbriz Auto,Parbrize Auto,Parbriz Auto Bucuresti,Parbrize Auto Bucuresti,Parbriz La Domiciliu,&lt;br /&gt;Parbrize La Domiciliu,Montaj Parbriz,Inlocuire Parbriz,&lt;br /&gt;Parbriz Pret,Parbriz Ieftin,Parbriz Bucuresti,Parbriz Ilfov&lt;br /&gt;VAND - LIVREZ - MONTEZ Bucuresti si Ilfov parbriz si geam &lt;br /&gt;auto orice model.&lt;br /&gt;parbriz cu senzori si fara, geam pentru vehicule noi si modele mai vechi, geamuri laterale, geam luneta , reparatii parbriz sau &lt;br /&gt;geam lateral, geam luneta. produse noi si aftermarket, parbriz la comanda - programare modele si tipuri geam peste 600 modele de &lt;br /&gt;parbrize si geamuri auto: Dacia, Ford, VW, BMW, Opel, Skoda, Nissan, Mercedes,&lt;br /&gt;Audi, Hyundai, Citroen, Mazda, Fiat, Peugeot, Honda, Subaru, Mitshubishi, &lt;br /&gt;Chevrolet, GMC, Alfa Romeo, Susuki, Toyota, Chrysler, Daewoo, Jeep,&lt;br /&gt;KIA, Lada, Lancia, Land Rover, Porche, Renault, Rover, SAAB,&lt;br /&gt;Seat, Smart, SsangYong, Volvo&lt;br /&gt;Inlocuire geam auto la domiciliu parbriz, luneta geam spate sau geam &lt;br /&gt;lateral Parbrizul este geamul auto&lt;br /&gt;care necesita, spre deosebire de alte geamuri auto, o mare atentie si materiale de calitate.&lt;br /&gt;Specialistii nostri au experienta necesara si dotarea &lt;br /&gt;tehnica sa rezolve orice montaj pentru un parbriz. Pentru geamul auto lateral, cu toate ca este un pic mai complicat deoarece &lt;br /&gt;are si sitemul mecanic si electric de coborare si ridicare a geamului, montajul este abordabil chiar si la domiciliul tau, echipa noastra tehnica &lt;br /&gt;fiind echipata corespunzator pentru acest tip de interventie &lt;br /&gt;Vand Livrez Montez la domiciliu geam auto Toata gama de geamuri &lt;br /&gt;auto&lt;br /&gt;Luneta auto, care obligatoriu are sistem anti inghetare, trebuie montata cu grija, pentru a nu periclita acest sistem de dezghet.Geam auto securizat cu laminare.&lt;br /&gt;Geamul de la fereastra de acasa - un lucru obisnuit,&lt;br /&gt;clar, puternic, ieftin. Dar cand este vorba de supra - solicitare, &lt;br /&gt;mai ales la impact, se dovedeste a fi fragila, spargandu-se in fragmente lungi &lt;br /&gt;si taiose, periculoase. Am putea folosi materiale plastice &lt;br /&gt;transparente, din pacate foarte rezistente la impact, &lt;br /&gt;la solicitari dure, dar foarte sensibile la matuire, zgriere.&lt;br /&gt;Pentru geamurile auto si ferestrele laterale auto s-a descoperit o solutie acum ceva ani in urma: &lt;br /&gt;sticla calita sau sticla securizata. Este mai rezistenta decat cea clasica dar mai important, cand &lt;br /&gt;se sparge, se transforma in granule mici, mai putin periculoase ca fasiile lungi si taioase de sticla.&lt;br /&gt;Cu toate acestea, sticla securizata pentru parbriz si geam auto, bombardata continuu cu fragmente &lt;br /&gt;minuscule la viteze foarte mari nu mai este cea mai buna solutie.&lt;br /&gt;&lt;br /&gt;Acum ceva timp producatorii de geamuri auto au inventat un sandwich din sticla si folie plastic.&lt;br /&gt;doua coli de sticla subtire sunt topite si imbraca o folie foarte subtire de cauciuc transparent.&lt;br /&gt;Straturile exterioare ale sticlei termice sunt independente unele de altele.&lt;br /&gt;&lt;br /&gt;stratul de cauciuc asigura o amortizare a undelor de soc, &lt;br /&gt;reducand probabilitatea spargerii geamului. Deasemena are rolul de a &lt;br /&gt;retine cioburile de sticla in cazul spargeri, astfel geamul auto nu se mai imprastie &lt;br /&gt;in mi de mici cioburi. &lt;br /&gt;Montaj parbriz auto la domiciliu&lt;br /&gt;Parbriz cu senzor sau parbriz simplu, geam lateral fata, geam sfert, geam luneta .Toata gama de geamuri securit pentru orice model de &lt;br /&gt;autovehicul. Suna acum !!! &lt;br /&gt;CONDUCI IN SIGURANTA&lt;br /&gt;Alege tipul de PARBRIZ, LUNETA GEAM AUTO LATERAL pentru modelul de masina pe care il ai.Cu noi poti repara &lt;br /&gt;(schimba) geam-ul auto fara sa te deplasezi. Suna, venim noi la &lt;br /&gt;tine !!! &lt;br /&gt;PARBRIZ SPART ?&lt;br /&gt;Nu Te Deplasezi. Venim Noi !&lt;br /&gt;COMANDA acum orice GEAM pentru modelul tau de vehicul,Echipa noastra vine la domiciliu si il MONTEAZA.&lt;br /&gt;&lt;br /&gt;http://parbriz-la-domiciliu.ro/index.html http://parbriz-la-domiciliu.ro/index.html&lt;/p&gt;

  • Image
    parbrize originale audi a4 b6 1.8
    February 03, 2020 Reply

    &lt;p&gt;Parbrize Audi ofera cea mai mare gama de parbrize auto din Romania.&lt;br /&gt;Vindem si montam la domiciliul clientului toate tipurile &lt;br /&gt;de parbrize de la producatori consacrati: Agc, Benson, Fuyao, Nordglass, Pilkinton, Saint-Gobain precum si parbrize originale.&lt;br /&gt;Detinem o retea de distributie si montaj in Bucuresti si Ilfov.&lt;br /&gt;&lt;br /&gt;PARBRIZ AUDI Agc, Benson, Fuyao, Nordglass, Pilkington, Saint-Gobain si Originale.&lt;br /&gt;Montam parbriz AUDI la domiciliul clientului in Bucuresti si &lt;br /&gt;Ilfov.&lt;br /&gt;Parbriz Audi,Parbrize Audi,Parbriz Auto Audi,Parbrize Auto Audi,Parbriz Auto &lt;br /&gt;Audi Bucuresti,Parbrize Auto Audi Bucuresti,Parbriz Audi La Domiciliu,Parbrize Audi La &lt;br /&gt;Domiciliu,Montaj Parbriz Audi,Inlocuire Parbriz Audi,Parbriz Pret,Parbriz Audi Ieftin,Parbriz Audi &lt;br /&gt;Bucuresti,Parbriz Audi Ilfov,Parbriz AUDI 100,Parbriz AUDI &lt;br /&gt;80,Parbriz AUDI 90,Parbriz AUDI A1,Parbriz AUDI A2,Parbriz AUDI A3,Parbriz AUDI A4,Parbriz AUDI A5,&lt;br /&gt;Parbriz AUDI A6,Parbriz AUDI A7,Parbriz AUDI A8,Parbriz AUDI &lt;br /&gt;ALLROAD,Parbriz AUDI CABRIOLET,Parbriz AUDI Q3,Parbriz AUDI Q5,Parbriz AUDI Q7,&lt;br /&gt;Parbriz AUDI QUATTRO,Parbriz AUDI R8,Parbriz AUDI TT,Parbriz AUDI V8,Parbriz AUDI Q8,Parbriz AUDI e Tron, &lt;br /&gt;&lt;br /&gt;PARBRIZ AUDI 80 B1 Sedan (80,82), PARBRIZ AUDI 80 B2 (81,&lt;br /&gt;85), PARBRIZ AUDI 80 B3 (89,89Q,8A), PARBRIZ AUDI 80 B4 &lt;br /&gt;Sedan (8C2), PARBRIZ AUDI 80 B4 Avant (8C5), PARBRIZ AUDI 90 B2 (81,85), PARBRIZ AUDI 90 B3 (89,&lt;br /&gt;89Q,8A), PARBRIZ AUDI 100 C1 Sedan, PARBRIZ AUDI 100 C1 &lt;br /&gt;Coupe, PARBRIZ AUDI 100 C2 Sedan (43), PARBRIZ AUDI 100 C2 Break &lt;br /&gt;(43), PARBRIZ AUDI 100 C3 Avant (44, 44Q), PARBRIZ AUDI 100 C3 Sedan (44, 44Q), PARBRIZ AUDI 100 C4 Sedan (4A2), PARBRIZ AUDI 100 C4 Avant (4A5), PARBRIZ AUDI 200 C2 Sedan (43), PARBRIZ AUDI 200 C3 Sedan (44, &lt;br /&gt;44Q), PARBRIZ AUDI 200 C3 Avant (44, 44Q), PARBRIZ AUDI A1 Hatchback (8X1, 8XK), &lt;br /&gt;PARBRIZ AUDI A1 Sportback (8XA, 8XK), PARBRIZ AUDI A1 Sportback (GBA), PARBRIZ AUDI &lt;br /&gt;A2 (8Z0), PARBRIZ AUDI A3 Hatchback (8L1), PARBRIZ AUDI A3 &lt;br /&gt;Hatchback (8P1), PARBRIZ AUDI A3 Sportback (8P1), PARBRIZ AUDI A3 Cabrio (8P7), PARBRIZ AUDI A3 Hatchback (8V1,8VK), PARBRIZ AUDI A3 Sportback (8VA,8VF), PARBRIZ AUDI A3 &lt;br /&gt;Sedan (8VS,8VM), PARBRIZ AUDI A3 Cabrio (8V7,8VE), PARBRIZ AUDI A4 Sedan (8D2), PARBRIZ AUDI A4 &lt;br /&gt;B5 Avant (8D5), PARBRIZ AUDI A4 B6,B7 Cabrio (8H7,8HE), PARBRIZ AUDI A4 B7 Sedan (8EC), PARBRIZ AUDI A4 &lt;br /&gt;B7 Avant (8ED), PARBRIZ AUDI A4 B8 Sedan (8K2), PARBRIZ AUDI A4 B8 &lt;br /&gt;Avant (8K5), PARBRIZ AUDI A4 B8 Allroad (8KH), PARBRIZ AUDI A4 B9 Sedan (8W2), PARBRIZ AUDI A4 B9 Avant (8W5), &lt;br /&gt;PARBRIZ AUDI A4 B9 Allroad (8WH), PARBRIZ AUDI A5 B8 Coupe (8T3), PARBRIZ AUDI A5 B8 &lt;br /&gt;Cabrio (8F7), PARBRIZ AUDI A5 B8 Sportback (8TA), PARBRIZ AUDI A5 &lt;br /&gt;B9 Coupe (F53), PARBRIZ AUDI A5 B9 Sportback (F5A), PARBRIZ AUDI A5 B9 Cabrio (F57),&lt;br /&gt;PARBRIZ AUDI A6 C4 Sedan (4A2), PARBRIZ AUDI A6 C4 Avant &lt;br /&gt;(4A5), PARBRIZ AUDI A6 C5 Sedan (4B2), PARBRIZ AUDI A6 C5 Avant (4B5), PARBRIZ AUDI A6 C6 Sedan (4F2),&lt;br /&gt;PARBRIZ AUDI A6 C6 Avant (4F5), PARBRIZ AUDI A6 &lt;br /&gt;C6 Allroad (4FH), PARBRIZ AUDI A6 C7 Sedan (4G2,4GC), PARBRIZ AUDI &lt;br /&gt;A6 C7 Avant (4G5,4GD), PARBRIZ AUDI A6 C7 Allroad (4GH,4GJ), PARBRIZ AUDI A6 C8 Sedan (4A2), PARBRIZ AUDI A6 C8 Avant (4A5), PARBRIZ AUDI A7 Sportback (4GA,4GF), &lt;br /&gt;PARBRIZ AUDI A7 Sportback (4KA), PARBRIZ AUDI A8 D2 (4D2,4D8), PARBRIZ AUDI &lt;br /&gt;A8 D3 (4E2,4E8), PARBRIZ AUDI A8 D4 (4H2,4H8,4HC,4HL), PARBRIZ AUDI &lt;br /&gt;A8 (4N2,4N8), PARBRIZ AUDI ALLROAD C5 (4BH), PARBRIZ AUDI CABRIOLET B4 (8G7), PARBRIZ AUDI COUPE B2 (81,&lt;br /&gt;85), PARBRIZ AUDI COUPE B3 (89,8B), PARBRIZ AUDI E TRON (GEN_), PARBRIZ &lt;br /&gt;AUDI Q2 (GAB), PARBRIZ AUDI Q3 (8UB,8UG), PARBRIZ AUDI Q3 (F3B), PARBRIZ &lt;br /&gt;AUDI Q5 (8RB), PARBRIZ AUDI Q5 (FYB), PARBRIZ AUDI Q7 (4LB), PARBRIZ AUDI Q7 (4MB), PARBRIZ AUDI Q8 (4MN), &lt;br /&gt;PARBRIZ AUDI QATTRO (85), PARBRIZ AUDI R8 Coupe (422,423), PARBRIZ AUDI R8 Spyder (427,429), PARBRIZ AUDI R8 Coupe (4S3,4SP), PARBRIZ AUDI R8 Spyder (4S9,&lt;br /&gt;4SR), PARBRIZ AUDI TT Coupe (8N3), PARBRIZ AUDI TT Roadster (8N9), PARBRIZ &lt;br /&gt;AUDI TT Coupe (8J3), PARBRIZ AUDI TT Roadster &lt;br /&gt;(8J9), PARBRIZ AUDI TT Coupe (FV3,FVP), PARBRIZ AUDI TT Roadster (FV9,FVR), PARBRIZ AUDI V8 (441,442,4C2,4C8), &lt;br /&gt;https://parbriz-audi.ro/parbriz-audi.html https://parbriz-audi.ro/parbriz-audi.html&lt;/p&gt;

  • Image
    Luneta Auto Ilfov
    January 21, 2020 Reply

    &lt;p&gt;Parbriz-auto-bucuresti.ro ofera cea mai mare gama de parbrize auto, lunete auto si geamuri auto din Romania.&lt;br /&gt;Vindem si montam la domiciliul clientului toate tipurile de parbrize,&lt;br /&gt;lunete si geamuri laterale de la producatori consacrati:&lt;br /&gt;Fuyao, Benson, Agc, Xyg, Saint-gobain, Guardian, Pilkinton, Splintex, Automotive, Nordglass, Steklo-lux,&lt;br /&gt;Doraglass si parbrize originale. Detinem o retea de distributie si montaj in Bucuresti &lt;br /&gt;si Ilfov&lt;br /&gt;Luneta,Lunete,Luneta Auto,Lunete Auto,Luneta &lt;br /&gt;Auto Bucuresti,Lunete Auto Bucuresti,Luneta &lt;br /&gt;La Domiciliu,Lunete La Domiciliu,Montaj Luneta Auto,Inlocuire Luneta Auto,Luneta Auto Pret,Luneta Auto Ieftina,Luneta Auto Bucuresti,Luneta Auto Ilfov&lt;br /&gt;Luneta unei masini este geamul din partea spate a masini.&lt;br /&gt;Exista mai multe tipuri de lunete: luneta &lt;br /&gt;cu dezaburire inclusa,luneta fara dezaburire, &lt;br /&gt;luneta clara, luneta verde, luneta verde inchis sau luneta fumurie.&lt;br /&gt;Parbrize Auto Bucuresti vinde, livreaza si monteaza la domiciliul clientului o gama foarte larga &lt;br /&gt;de lunete. Montam lunete la domiciliul clientului in Bucuresti si Ilfov.&lt;br /&gt;Lunete Originale, Pilkington, Fuyao, Benson. Lunete cu incalzire, lunete fumurii.&lt;br /&gt;Parbrize Auto Bucuresti practica cele mai mici preturi &lt;br /&gt;de pe piata, oferind in acelasi timp servicii de inalta calitate &lt;br /&gt;precum si o garantie de 2 ani pentru toate lunetele vandute si montate.&lt;br /&gt;&lt;br /&gt;Luneta AGC, Luneta BENSON, Luneta DORAGLASS, Luneta FUYAO, Luneta GUARDIAN, Luneta PILKINGTON, Luneta NORDGLASS,&lt;br /&gt;Luneta SAINT GOBAIN, Luneta XYG,&lt;br /&gt;https://parbriz-auto-bucuresti.ro/luneta.html https://parbriz-auto-bucuresti.ro/luneta.html&lt;/p&gt;