kins
kins

Reputation: 61

I get an error while creating a datatable in laravel

does anyone know why am I getting this error while creating datatable for my job category?

DatabaseSeeder.php

<?php


use Illuminate\Database\Seeder;

// Import DB and Faker services
use Illuminate\Support\Facades\DB;
use Faker\Factory as Faker;

class DatabaseSeeder extends Seeder
{
/**
 * Seed the application's database.
 *
 * @return void
 */
public function run()
{
    // \App\Models\User::factory(10)->create();

    $faker = Faker::create();

   

    foreach (range(1,150) as $index) {
        DB::table('jobs')->insert([
            'company' => $faker->company,
            'category' => $faker->category,
            'position' => $faker->position,
            'description' => $faker->description,
            'salary_from' => $faker->salary_from,
            'salary_to' => $faker->salary_to,
            'status' => $faker->status,

            
        ]);
   }
}  
}

my error is

 InvalidArgumentException 

Unknown formatter "category"

at C:\xampp\htdocs\job\vendor\fakerphp\faker\src\Faker\Generator.php:300
296▕                 return $this->formatters[$formatter];
297▕             }
298▕         }
299▕ 
➜ 300▕         throw new \InvalidArgumentException(sprintf('Unknown formatter "%s"', $formatter));
301▕     }
302▕ 
303▕     /**
304▕      * Replaces tokens ('{{ tokenName }}') with the result from the token method call

1   C:\xampp\htdocs\job\vendor\fakerphp\faker\src\Faker\Generator.php:278
  Faker\Generator::getFormatter("category")

2   C:\xampp\htdocs\ob\vendor\fakerphp\faker\src\Faker\Generator.php:497
  Faker\Generator::format("category")

This is my first time creating datatable, and i am new to laravel, so after checking i found that using datatable is much simpler to generate data

Upvotes: 0

Views: 1831

Answers (2)

sherifcoder
sherifcoder

Reputation: 169

category doesn't exists in faker , and also position (if it means the job position , there is job title that does the thing) , may be you want also to control the categories names so i would create an array

   DB::table('jobs')->insert([
            'company' => $faker->company,
            'category' => $faker->randomElement(['technology', 'business','law','accounting']),
            'position' => $faker->jobtitle(),
            'description' => $faker->text,
            'salary_from' => $faker->randomDigit,
            'salary_to' => $faker->randomDigit,
            'status' => $faker->numberBetween(1, 3), // Change to available statusses         
        ]);

Upvotes: 1

MaartenDev
MaartenDev

Reputation: 5811

You are using some fields that are not supported by faker. Checkout the available fields here: https://github.com/fzaninotto/Faker.

Checkout the example with updated fields here:

   DB::table('jobs')->insert([
            'company' => $faker->company,
            'category' => $faker->word,
            'position' => $faker->randomDigit,
            'description' => $faker->text,
            'salary_from' => $faker->randomDigit,
            'salary_to' => $faker->randomDigit,
            'status' => $faker->numberBetween(1, 3), // Change to available statusses         
        ]);

Upvotes: 2

Related Questions