Using Faker in Laravel 5

1

I have a problem using Faker to migrate data to my table. I want to import 50 fakes names and countrys to my table. When I execute the db:seed command, it does not do a% co_of% of anything.

Property model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Propiedad extends Model{

    protected $table = 'Propiedades';

    protected $fillable = ['name', 'pais'];
}

PropertiesTableSeeder

<?php

use Illuminate\Database\Seeder;
use  Faker\Factory as Faker;
use Illuminate\Support\Facades\DB;

class PropiedadesTableSeeder extends Seeder {
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run() {
        factory(App\Propiedad::class, 50)->create()->each(function($u){
            $u->posts()->save(factory(App\Propiedad::class)->make());
        });
    }
}

Table

<?php

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

class PropiedadesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(){
        Schema::create('propiedades', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nombre');
            $table->integer('barrio');
            $table->integer('ciudad');
            $table->integer('pais');
            $table->boolean('cochera');
            $table->float('ambientes');
            $table->timestamps();
        });
    }

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

databaseseeder

    <?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder {
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run() {
//        $this->call(DatabaseSeeder::class);
//        $this->call(PropiedadesTableSeeder::class);
    }
}

factory

<?php

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/

$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;

    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),
    ];
});

$factory->define(App\Propiedad::class, function ($faker) {
    return [
        'nombre' => $faker->name,
        'pais' => $faker->unique()->country,
    ];
});
    
asked by Infraganti 17.11.2016 в 22:59
source

3 answers

1

In theory it should work only with this line in the PropertiesTableSeeder:

factory(App\Propiedad::class, 50)->create();

When using it in the current way, what you are doing is adding a Property-Property relationship, which I think does not exist (and you do not need either).

You should also remove the comment from DatabaseSeeder:

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder {
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run() {
        // $this->call(DatabaseSeeder::class);
        $this->call(PropiedadesTableSeeder::class);
    }
}
    
answered by 18.11.2016 / 01:06
source
0

My postSeeder

class PostsMediaSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('media')->truncate();

        $faker = Faker\Factory::create();
        foreach (\App\Post::all() as $post) {
            if(rand(1, 10) > 4){
                $counter = 0;
                // max retries = 5 because sometimes faker return false
                while (!($fakeImage = $faker->image(null, 600, 400)) && ($counter < 5)) {
                    $counter++;
                }

                if ($fakeImage !== false) {
                    $post->addMedia($fakeImage)->preservingOriginal()->toCollectionOnDisk('featured', 'local-media');
                }
            }
        }
    }
}

My databaseSeeder

 <?php

    use Illuminate\Database\Seeder;

    class DatabaseSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            if (app()->environment() == 'local') {
                DB::statement('SET FOREIGN_KEY_CHECKS=0;');
                $this->call(PostsSeeder::class);
                DB::statement('SET FOREIGN_KEY_CHECKS=1;');
            }

        }
    }

My factory in this way I work my seeder

$factory->define(App\Post::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->sentence(5),
        'description' => $faker->sentences(2, true),
        'content' => markdownContent($faker),
        'created_by' => App\User::admin()->first()->id,
        'status' => \Hootlex\Moderation\Status::APPROVED,
        'moderated_at' => time()
    ];
});
    
answered by 18.11.2016 в 00:55
0

I had changed the names of the schema and my faker. For example in my faker said name and in the column said name. I also had a data type error. For example, in the country type column I had assigned (integer), because I wanted to id but what the faker brought me was a string. Now everything is moving

public function up(){
    Schema::create('propiedades', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nombre');
        $table->string('ciudad');
        $table->string('direccion');
        $table->string('pais');
        $table->integer('barrio');
        $table->boolean('cochera');
        $table->float('ambientes');
        $table->integer('superfice');
        $table->timestamps();
    });
}

ModelFactory

$factory->define(App\Propiedad::class, function (Faker\Generator $faker) {
    return [
        'nombre' => $faker->name,
        'pais' => $faker->country,
        'ciudad' => $faker->city,
        'direccion' => $faker->address,
    ];
});

So I stay in my sql

    
answered by 19.11.2016 в 02:09