0

I am getting the following error when trying to seed in Laravel 5.4

 [Symfony\Component\Debug\Exception\FatalThrowableError]                                            
Parse error: syntax error, unexpected '$faker' (T_VARIABLE), expecting function (T_FUNCTION) or c  
onst (T_CONST)

Here is the code for the seed file.

<?php

use Illuminate\Database\Seeder;
use App\Book;
use Faker\Factory as Faker;

class BookSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
 $faker = Faker::create();

public function run()
{
  foreach (range(1, 30) as $index) {
    Book::create([
      'title'=> $faker->sentence(5),
      'author'=> $faker->sentence(7),
      'description'=>$faker->paragraph(4)
    ]);
  }
}

}

I have created the model and done the migration. I can't seem to find any good tutorials on how to do this with Laravel 5.4 . Any help would be appreciated.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
AltBrian
  • 2,392
  • 9
  • 29
  • 58

1 Answers1

2

Declare $faker variable in run method will solve the problem

`<?php
   use Illuminate\Database\Seeder; 
   use App\Book; use Faker\Factory as Faker; 

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

       $faker = Faker::create(); 

       foreach (range(1, 30) as $index) { 
            Book::create([ 
                    'title'=> $faker->sentence(5), 
                    'author'=> $faker->sentence(7), 
                    'description'=>$faker->paragraph(4) 
                   ]); 
       } 
   }
  }`
Chintan7027
  • 7,115
  • 8
  • 36
  • 50