DB seeder doesen’t function why?

  laravel, php

When i do db:seed MembersSeeder it doesen’t works because im currently trying to do it with laravel 8 but the turtorial seems to be in laravel 5.8. Could you be so nice and find me a fix for this current problem please ( fix that works or even solution ).

DatabaseSeeder


   ParseError 

  syntax error, unexpected 'foreach' (T_FOREACH)

  at database/seeders/MembersSeeder.php:19
     15▕      */
     16▕     public function run()
     17▕     {
     18▕         $faker = Faker::create(){
  ➜  19▕         foreach(range(1,100) as $index)
     20▕             DB::table('profiles_')->insert([
     21▕             'id' =>$faker->idNumber->from(1,100),
     22▕             'user_id' =>$faker->idNumber,
     23▕             'address' =>$faker->address,

      +1 vendor frames 
  2   [internal]:0
      ComposerAutoloadClassLoader::loadClass("DatabaseSeedersMembersSeeder")

  3   [internal]:0
      spl_autoload_call("DatabaseSeedersMembersSeeder")```

MembersSeeder

<?php

namespace DatabaseSeeders;

use IlluminateDatabaseSeeder;
use IlluminateSupportFacadesDB;
use FakerFactory as Faker;

class MembersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker::create(){
        foreach(range(1,100) as $index)
            DB::table('profiles_')->insert([
            'id' =>$faker->idNumber->from(1,100),
            'user_id' =>$faker->idNumber,
            'address' =>$faker->address,
            'created_at' => Str::random(10),
            'updated_at' => Str::random(10),
            ]);
        }
    }
}

UserFactory.php

<?php

namespace DatabaseFactories;

use AppModelsUser;
use IlluminateDatabaseEloquentFactoriesFactory;
use IlluminateSupportStr;

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }
}

Migration Table

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles_', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id');
            $table->string('address');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('profiles_');
    }
}

Error

[email protected] igel % php artisan db:seed MembersSeeder

   ParseError 

  syntax error, unexpected 'foreach' (T_FOREACH)

  at database/seeders/MembersSeeder.php:19
     15▕      */
     16▕     public function run()
     17▕     {
     18▕         $faker = Faker::create(){
  ➜  19▕         foreach(range(1,100) as $index)
     20▕             DB::table('profiles_')->insert([
     21▕             'id' =>$faker->idNumber->from(1,100),
     22▕             'user_id' =>$faker->idNumber,
     23▕             'address' =>$faker->address,

      +1 vendor frames 
  2   [internal]:0
      ComposerAutoloadClassLoader::loadClass("DatabaseSeedersMembersSeeder")

  3   [internal]:0
      spl_autoload_call("DatabaseSeedersMembersSeeder")

Source: Laravel

Leave a Reply