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?