0

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

2 Answers 2

2

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         
        ]);
Sign up to request clarification or add additional context in comments.

1 Comment

Just a side note that Laravel 8 uses the FakerPHP library rather than the fzaninotto library of previous versions.
1

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         
        ]);

2 Comments

what if the category is supposed to be filled by the admin? should is till create an array?
no but let's say this is a job posting website , some categories will be the same , because if he create only random word , no category will be the same , i supposed he wants also to create a category page with jobs from the same category. But normally here i would have done another table for category with one to many (one category has many jobs) relation , so i would have created seeder for categories , with the categories i would choose and then seed the jobs and fill category_id with numberbetween

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.