samedi 27 octobre 2018

Laravel Model Factory Error: Trying to get property of non-object

I'm trying to use a model factory to seed my database, but when I run it I get the error:

Trying to get property 'id' of non-object

Here is my code:

// TasksTableSeeder.php

factory(pams\Task::class, '2000', rand(1, 30))->create();

// ModelFactory.php

$factory->defineAs(pams\Task::class, '2000', function (Faker\Generator $faker) {
static $task_number = 01;
return [
    'task_number' => $task_number++,
    'ata_code' => '52-00-00',
    'time_estimate' => $faker->randomFloat($nbMaxDecimals = 2, $min = 0.25, $max = 50),
    'work_order_id' => '2000',
    'description' => $faker->text($maxNbChars = 75),
    'action' => '',
    'duplicate' => '0',
    'certified_by' => '1',
    'certified_date' => '2015-11-08',
    'status' => '1',
    'created_by' => '1',
    'modified_by' => '1',
    'created_at' => Carbon\Carbon::now()->format('Y-m-d H:i:s'),
    'updated_at' => Carbon\Carbon::now()->format('Y-m-d H:i:s'),
    ];
});

I've tried removing all variables from the model factory and using constants, but that doesn't fix it. I've tried pulling the data from the ModelFactory.php and put it directly into the TasksTableSeeder.php and it does work, however I was using constants and no variables.

I cannot for the life of me figure out what 'id' it's talking about.

I'm running Laravel v5.1



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire