How to generate dummy data using Faker in Laravel

  8 months ago     Views:  805  
Julius Braun
1 Follows


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

8 Comments

  • Image
    content6-ml
    May 18, 2020 Reply

    Hello there! This post couldn't be written any better!
    Reading this post reminds me of my previous room mate!
    He always kept talking about this. I will
    forward this article to him. Pretty sure he will have a good read.
    Many thanks for sharing! http://parbriz-la-domiciliu.ro/parbriz-chrysler.html

  • Image
    Parbriz CHEVROLET Bucuresti
    April 29, 2020 Reply

    I was recommended this web site by my cousin. I'm not sure whether this post is written by him as no one else know such detailed about
    my difficulty. You are amazing! Thanks! https://parbrize-la-domiciliu.ro/index.php?p=parbrize&m=Chevrolet&f=chevrolet.jpg&alt=Parbriz

  • Image
    Parbriz Chrysler Voyager
    April 18, 2020 Reply

    Wonderful post however , I was wondering if
    you could write a litte more on this topic? I'd be very grateful if you could elaborate a little bit more.

    Thank you! https://parbriz-bucuresti.ro/index.html

  • Image
    content7-1a
    April 13, 2020 Reply

    Valuable information. Fortunate me I discovered your site by
    chance, and I'm shocked why this coincidence did not came about earlier!
    I bookmarked it. http://parbriz-la-domiciliu.ro/geam-auto-parbriz-dacia.html

  • Image
    content6-ao
    April 08, 2020 Reply

    We are a group of volunteers and starting a new scheme in our
    community. Your website provided us with valuable info to work on. You've
    done a formidable job and our whole community will be thankful to you. http://parbriz-la-domiciliu.ro/parbriz-audi.html

  • Image
    Parbriz
    February 21, 2020 Reply

    Vand parbriz Bucuresti, geam luneta, geam lateral
    Servicii calificate de inlocuire a oricarui geam auto la domiciliu
    - 2 ani garantie -
    Parbriz,Parbrize,Parbriz Auto,Parbrize Auto,Parbriz Auto Bucuresti,Parbrize Auto Bucuresti,Parbriz La Domiciliu,
    Parbrize La Domiciliu,Montaj Parbriz,Inlocuire Parbriz,
    Parbriz Pret,Parbriz Ieftin,Parbriz Bucuresti,Parbriz Ilfov
    VAND - LIVREZ - MONTEZ Bucuresti si Ilfov parbriz si geam
    auto orice model.
    parbriz cu senzori si fara, geam pentru vehicule noi si modele mai vechi, geamuri laterale, geam luneta , reparatii parbriz sau
    geam lateral, geam luneta. produse noi si aftermarket, parbriz la comanda - programare modele si tipuri geam peste 600 modele de
    parbrize si geamuri auto: Dacia, Ford, VW, BMW, Opel, Skoda, Nissan, Mercedes,
    Audi, Hyundai, Citroen, Mazda, Fiat, Peugeot, Honda, Subaru, Mitshubishi,
    Chevrolet, GMC, Alfa Romeo, Susuki, Toyota, Chrysler, Daewoo, Jeep,
    KIA, Lada, Lancia, Land Rover, Porche, Renault, Rover, SAAB,
    Seat, Smart, SsangYong, Volvo
    Inlocuire geam auto la domiciliu parbriz, luneta geam spate sau geam
    lateral Parbrizul este geamul auto
    care necesita, spre deosebire de alte geamuri auto, o mare atentie si materiale de calitate.
    Specialistii nostri au experienta necesara si dotarea
    tehnica sa rezolve orice montaj pentru un parbriz. Pentru geamul auto lateral, cu toate ca este un pic mai complicat deoarece
    are si sitemul mecanic si electric de coborare si ridicare a geamului, montajul este abordabil chiar si la domiciliul tau, echipa noastra tehnica
    fiind echipata corespunzator pentru acest tip de interventie
    Vand Livrez Montez la domiciliu geam auto Toata gama de geamuri
    auto
    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.
    Geamul de la fereastra de acasa - un lucru obisnuit,
    clar, puternic, ieftin. Dar cand este vorba de supra - solicitare,
    mai ales la impact, se dovedeste a fi fragila, spargandu-se in fragmente lungi
    si taiose, periculoase. Am putea folosi materiale plastice
    transparente, din pacate foarte rezistente la impact,
    la solicitari dure, dar foarte sensibile la matuire, zgriere.
    Pentru geamurile auto si ferestrele laterale auto s-a descoperit o solutie acum ceva ani in urma:
    sticla calita sau sticla securizata. Este mai rezistenta decat cea clasica dar mai important, cand
    se sparge, se transforma in granule mici, mai putin periculoase ca fasiile lungi si taioase de sticla.
    Cu toate acestea, sticla securizata pentru parbriz si geam auto, bombardata continuu cu fragmente
    minuscule la viteze foarte mari nu mai este cea mai buna solutie.

    Acum ceva timp producatorii de geamuri auto au inventat un sandwich din sticla si folie plastic.
    doua coli de sticla subtire sunt topite si imbraca o folie foarte subtire de cauciuc transparent.
    Straturile exterioare ale sticlei termice sunt independente unele de altele.

    stratul de cauciuc asigura o amortizare a undelor de soc,
    reducand probabilitatea spargerii geamului. Deasemena are rolul de a
    retine cioburile de sticla in cazul spargeri, astfel geamul auto nu se mai imprastie
    in mi de mici cioburi.
    Montaj parbriz auto la domiciliu
    Parbriz cu senzor sau parbriz simplu, geam lateral fata, geam sfert, geam luneta .Toata gama de geamuri securit pentru orice model de
    autovehicul. Suna acum !!!
    CONDUCI IN SIGURANTA
    Alege tipul de PARBRIZ, LUNETA GEAM AUTO LATERAL pentru modelul de masina pe care il ai.Cu noi poti repara
    (schimba) geam-ul auto fara sa te deplasezi. Suna, venim noi la
    tine !!!
    PARBRIZ SPART ?
    Nu Te Deplasezi. Venim Noi !
    COMANDA acum orice GEAM pentru modelul tau de vehicul,Echipa noastra vine la domiciliu si il MONTEAZA.

    http://parbriz-la-domiciliu.ro/index.html http://parbriz-la-domiciliu.ro/index.html

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

    Parbrize Audi ofera cea mai mare gama de parbrize auto din Romania.
    Vindem si montam la domiciliul clientului toate tipurile
    de parbrize de la producatori consacrati: Agc, Benson, Fuyao, Nordglass, Pilkinton, Saint-Gobain precum si parbrize originale.
    Detinem o retea de distributie si montaj in Bucuresti si Ilfov.

    PARBRIZ AUDI Agc, Benson, Fuyao, Nordglass, Pilkington, Saint-Gobain si Originale.
    Montam parbriz AUDI la domiciliul clientului in Bucuresti si
    Ilfov.
    Parbriz Audi,Parbrize Audi,Parbriz Auto Audi,Parbrize Auto Audi,Parbriz Auto
    Audi Bucuresti,Parbrize Auto Audi Bucuresti,Parbriz Audi La Domiciliu,Parbrize Audi La
    Domiciliu,Montaj Parbriz Audi,Inlocuire Parbriz Audi,Parbriz Pret,Parbriz Audi Ieftin,Parbriz Audi
    Bucuresti,Parbriz Audi Ilfov,Parbriz AUDI 100,Parbriz AUDI
    80,Parbriz AUDI 90,Parbriz AUDI A1,Parbriz AUDI A2,Parbriz AUDI A3,Parbriz AUDI A4,Parbriz AUDI A5,
    Parbriz AUDI A6,Parbriz AUDI A7,Parbriz AUDI A8,Parbriz AUDI
    ALLROAD,Parbriz AUDI CABRIOLET,Parbriz AUDI Q3,Parbriz AUDI Q5,Parbriz AUDI Q7,
    Parbriz AUDI QUATTRO,Parbriz AUDI R8,Parbriz AUDI TT,Parbriz AUDI V8,Parbriz AUDI Q8,Parbriz AUDI e Tron,

    PARBRIZ AUDI 80 B1 Sedan (80,82), PARBRIZ AUDI 80 B2 (81,
    85), PARBRIZ AUDI 80 B3 (89,89Q,8A), PARBRIZ AUDI 80 B4
    Sedan (8C2), PARBRIZ AUDI 80 B4 Avant (8C5), PARBRIZ AUDI 90 B2 (81,85), PARBRIZ AUDI 90 B3 (89,
    89Q,8A), PARBRIZ AUDI 100 C1 Sedan, PARBRIZ AUDI 100 C1
    Coupe, PARBRIZ AUDI 100 C2 Sedan (43), PARBRIZ AUDI 100 C2 Break
    (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,
    44Q), PARBRIZ AUDI 200 C3 Avant (44, 44Q), PARBRIZ AUDI A1 Hatchback (8X1, 8XK),
    PARBRIZ AUDI A1 Sportback (8XA, 8XK), PARBRIZ AUDI A1 Sportback (GBA), PARBRIZ AUDI
    A2 (8Z0), PARBRIZ AUDI A3 Hatchback (8L1), PARBRIZ AUDI A3
    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
    Sedan (8VS,8VM), PARBRIZ AUDI A3 Cabrio (8V7,8VE), PARBRIZ AUDI A4 Sedan (8D2), PARBRIZ AUDI A4
    B5 Avant (8D5), PARBRIZ AUDI A4 B6,B7 Cabrio (8H7,8HE), PARBRIZ AUDI A4 B7 Sedan (8EC), PARBRIZ AUDI A4
    B7 Avant (8ED), PARBRIZ AUDI A4 B8 Sedan (8K2), PARBRIZ AUDI A4 B8
    Avant (8K5), PARBRIZ AUDI A4 B8 Allroad (8KH), PARBRIZ AUDI A4 B9 Sedan (8W2), PARBRIZ AUDI A4 B9 Avant (8W5),
    PARBRIZ AUDI A4 B9 Allroad (8WH), PARBRIZ AUDI A5 B8 Coupe (8T3), PARBRIZ AUDI A5 B8
    Cabrio (8F7), PARBRIZ AUDI A5 B8 Sportback (8TA), PARBRIZ AUDI A5
    B9 Coupe (F53), PARBRIZ AUDI A5 B9 Sportback (F5A), PARBRIZ AUDI A5 B9 Cabrio (F57),
    PARBRIZ AUDI A6 C4 Sedan (4A2), PARBRIZ AUDI A6 C4 Avant
    (4A5), PARBRIZ AUDI A6 C5 Sedan (4B2), PARBRIZ AUDI A6 C5 Avant (4B5), PARBRIZ AUDI A6 C6 Sedan (4F2),
    PARBRIZ AUDI A6 C6 Avant (4F5), PARBRIZ AUDI A6
    C6 Allroad (4FH), PARBRIZ AUDI A6 C7 Sedan (4G2,4GC), PARBRIZ AUDI
    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),
    PARBRIZ AUDI A7 Sportback (4KA), PARBRIZ AUDI A8 D2 (4D2,4D8), PARBRIZ AUDI
    A8 D3 (4E2,4E8), PARBRIZ AUDI A8 D4 (4H2,4H8,4HC,4HL), PARBRIZ AUDI
    A8 (4N2,4N8), PARBRIZ AUDI ALLROAD C5 (4BH), PARBRIZ AUDI CABRIOLET B4 (8G7), PARBRIZ AUDI COUPE B2 (81,
    85), PARBRIZ AUDI COUPE B3 (89,8B), PARBRIZ AUDI E TRON (GEN_), PARBRIZ
    AUDI Q2 (GAB), PARBRIZ AUDI Q3 (8UB,8UG), PARBRIZ AUDI Q3 (F3B), PARBRIZ
    AUDI Q5 (8RB), PARBRIZ AUDI Q5 (FYB), PARBRIZ AUDI Q7 (4LB), PARBRIZ AUDI Q7 (4MB), PARBRIZ AUDI Q8 (4MN),
    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,
    4SR), PARBRIZ AUDI TT Coupe (8N3), PARBRIZ AUDI TT Roadster (8N9), PARBRIZ
    AUDI TT Coupe (8J3), PARBRIZ AUDI TT Roadster
    (8J9), PARBRIZ AUDI TT Coupe (FV3,FVP), PARBRIZ AUDI TT Roadster (FV9,FVR), PARBRIZ AUDI V8 (441,442,4C2,4C8),
    https://parbriz-audi.ro/parbriz-audi.html https://parbriz-audi.ro/parbriz-audi.html

  • Image
    Luneta Auto Ilfov
    January 21, 2020 Reply

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

    Luneta AGC, Luneta BENSON, Luneta DORAGLASS, Luneta FUYAO, Luneta GUARDIAN, Luneta PILKINGTON, Luneta NORDGLASS,
    Luneta SAINT GOBAIN, Luneta XYG,
    https://parbriz-auto-bucuresti.ro/luneta.html https://parbriz-auto-bucuresti.ro/luneta.html