0

Following Your First Laravel application tutorial on laravel-news.com I came to a point where I need to edit LinkFactory.php created by $ php artisan make:model --factory Link.

The default code created by make is the following:

<?php

namespace Database\Factories;

use App\Models\Link;
use Illuminate\Database\Eloquent\Factories\Factory;

class LinkFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Link::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            //
        ];
    }
}

And the tutorial suggest to replace that code with the following:

<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Link;
use Faker\Generator as Faker;
$factory->define(Link::class, function (Faker $faker) {
    return [
        'title' => substr($faker->sentence(2), 0, -1),
        'url' => $faker->url,
        'description' => $faker->paragraph,
    ];
});

I understand some namespacing differences ie. App\Link -> App\Models\Link and \Illuminate\Database\Eloquent\Factory -> Illuminate\Database\Eloquent\Factories\Factory and the need to add Faker\Generator as Faker, but I'm not sure what to do with the rest.

I'm thinking something like the following:

<?php

namespace Database\Factories;

use App\Models\Link;
use Illuminate\Database\Eloquent\Factories\Factory;
use Faker\Generator as Faker;

class LinkFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Link::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $faker = new Faker;
        
        return [
            'title' => substr($faker->sentence(2), 0, -1),
            'url' => $faker->url,
            'description' => $faker->paragraph,
        ];
    }
}

First, I need to know if this is correct, and how it is not.

Second, I don't know if I need to use the $model property anywhere here.

And third, how does that affect any further usage elsewhere in code?

s3c
  • 1,481
  • 19
  • 28

1 Answers1

0

Now that I found this answer about Undefined factory() helper function, I think my question may be marked as [duplicate].

This is working now.

LinkFactory.php

<?php

namespace Database\Factories;

use App\Models\Link;
use Illuminate\Database\Eloquent\Factories\Factory;

class LinkFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Link::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {        
        return [
            'title' => substr($this->faker->sentence(2), 0, -1),
            'url' => $this->faker->url,
            'description' => $this->faker->paragraph,
        ];
    }
}

LinksTableSeeder.php

<?php

namespace Database\Seeders;

use App\Models\Link;
use Illuminate\Database\Seeder;

class LinksTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Link::factory()->count(30)->create();
    }
}

I also had to change DatabaseSeeder.php to

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(LinksTableSeeder::class);
    }
}

and then ran

$ php artisan migrate:fresh --seed

and the database was populated.

s3c
  • 1,481
  • 19
  • 28