Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factories on Laravel 5.2 not working as expected

Tags:

php

laravel-5

I have a very strange problem with Laravel 5.2 factories.

I have recently upgraded from Laravel 5.1 to 5.2 following the upgrade guide on the Laravel website. All works as exepected except one factory. Yes the others work ok. Here are two of the factories:

$factory->define(App\Client::class, function (Faker\Generator $faker) {
    return [
        'name'              => $faker->company,
        'building'          => $faker->buildingNumber,
        'street'            => $faker->streetName,
        'town'              => $faker->city,
        'postcode'          => $faker->postcode,
        'country'           => 'UK',
        'telephone'         => $faker->phoneNumber,
        'fax'               => $faker->phoneNumber,
    ];
});

$factory->define(App\Shift::class, function (Faker\Generator $faker) {
    return [
        'client_id'         => $faker->numberBetween($min = 1, $max = 15),
        'user_id'           => $faker->numberBetween($min = 1, $max = 15),
        'start'             => $faker->dateTimeBetween($startDate='now', $endDate='+60 days'),
        'public'            => $faker->boolean(),
    ];
});

The top factory works no problem but the second one doesn't run at all cause my db seed to throw an error because its not populating the client_id which is a foreign key.

The only difference between the two models is that the client model doesn't use timestamps where as the shift model does. Other than that they are identical.

I will keep plugging away but any help to shed light on this would be greatly received.

like image 882
Gavin Avatar asked Nov 20 '25 11:11

Gavin


1 Answers

When you add your own constructor, are you making sure to call parent::__construct() inside it?

like image 181
Viral Solani Avatar answered Nov 23 '25 00:11

Viral Solani