0

My ModelFactory:

<?php        

    $factory->define(App\Models\Customer::class, function (Faker\Generator $faker) {
        return [
            'name' => $faker->company,
            'email' => $faker->unique()->safeEmail,
            'status'=> $faker->numberBetween($min = 0, $max = 2),
            'slug'=> $faker->slug,
        ];
    });

Database seeder

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

        $customers= factory(App\Models\Customer::class, 100)->create();

    }
}

When I run

php artisan db:seed

I get the error

  [Symfony\Component\Debug\Exception\FatalThrowableError]
  Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting '
  ]'

I've tried everything I can think of, but can't find the problem...


Edit:

I forgot to mention that this was working fine a day earlier and then "broke" as I started to add more ModelFactories (in separate files). I then discarded all my changes (from source control) to be 100% sure I hadn't changed anything. The only other aspect could be that I have something in the .gitignore that may have updated and wasn't rolled back:

/node_modules
/public/storage
/public/hot
/storage/*.key
/.idea
Homestead.json
Homestead.yaml
seekay
  • 2,493
  • 3
  • 27
  • 33
  • I don't see any syntax errors in the code you have posted. From the error message, though, it looks like you have an unclosed array somewhere. – patricus Mar 02 '17 at 08:43

2 Answers2

1

It seems the problem is this line:

'status'=> $faker->numberBetween($min = 0, $max = 2),

It should be:

'status'=> $faker->numberBetween(0, 2),
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • While probably not intended, that shouldn't be a syntax error. It is just assigning two variables as they are passed into the `numberBetween()` method. – patricus Mar 02 '17 at 08:38
  • @Marcin, still get the same error. Updated the question with some additional background info as well – seekay Mar 02 '17 at 17:25
0

Ok, so I found the reason and feel really idiotic about it, but posting it here anyway in case someone follows in my footsteps.

The issue was that there were other ModelFactories in the database/factories folder and it seems that running php artisan db:seed parses those files as well, even though they are not referenced in the DatabaseSeeder class. One of those files had incorrect one-many syntax and that was causing the error.

The only way I realized it was that I ran the factory->create method within php artisan tinker and the error message it threw up referenced this other factory definition.

FWIW, I then used the approach outlined here for my relationships - for the reasons mentioned in the question there...

Community
  • 1
  • 1
seekay
  • 2,493
  • 3
  • 27
  • 33