Questions tagged [laravel-seeding]

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish but probably should follow some sensible convention, such as UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

Example Database Seed Class

class DatabaseSeeder extends Seeder {

    public function run()
    {
        $this->call('UserTableSeeder');

        $this->command->info('User table seeded!');
    }

}

class UserTableSeeder extends Seeder {

    public function run()
    {
        DB::table('users')->delete();

        User::create(['email' => 'foo@bar.com']);
    }

}

To seed your database, you may use the db:seed command on the Artisan CLI:

php artisan db:seed

By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:

php artisan db:seed --class=UserTableSeeder

You may also seed your database using the migrate:refresh command, which will also rollback and re-run all of your migrations:

php artisan migrate:refresh --seed
316 questions
0
votes
1 answer

Laravel ,php -artisan suffixe had to my table when i seed it

I have 2 small issues with the "php artisan db:seed" command. WHen i run the command, i have this error message : "SQLSTATE[42S02] Base table or view not found : 1146 La table "bootstrap_template_commerciauxes" n'existe pas ..." The problem is :…
Fares
  • 3
  • 3
0
votes
1 answer

Artisan::call('migrate:fresh') on php script making the site unavailable for the first time and works on next reload

I am using the below method private function migrate(BufferedOutput $outputLog) { try { Artisan::call('migrate:fresh', $outputLog); } catch (Exception $e) { return $this->response($e->getMessage(), 'error',…
0
votes
3 answers

laravel 8 admin seeder dosn't seed filed email dosn't have a default value

i am trying to insert values to admin seeder but it dosn't seed i don't know why i wrote everything correct and my error is General error: 1364 Field 'email' doesn't have a default value") and i don't want to make default value i just want to seed…
MEDO bro
  • 45
  • 5
0
votes
1 answer

ErrorException: Array to string conversion. When trying to createMany() on a hasMany relationship

I've been screwing around with this a lot and can not seem to figure it out. I scraped foursquare for their categories and am trying to use them to seed categories in my DB. I am using PHP and Laravel. Here is the code for the CategorySeeder: class…
Twoscore
  • 85
  • 2
  • 6
0
votes
0 answers

In Laravel while Seeding tables it throw error: with property [id] does not exist

I can't seed table with factory(num)->create(). $user = User::factory(10)->create(); It is show me error message : but I can seed the table with this statement $user = User::create([ 'name' => 'noraddin','email' => 'noordeen@gmail.com',…
Noraddeen
  • 37
  • 5
0
votes
1 answer

Class "App\Http\Controllers\Survey" not found

Good day. My group our currently working in our final project using Laravel. I have this problem that I encounter.
0
votes
1 answer

Saving seeding data to database in Laravel

I am participating in a Laravel course. The course was supposed to be laravel 8, but it is 30/70 laravel 8, and laravel 5 was outdated and is slowly updated. The problem is saving seeding data to a database (MySQL). I can save Users to the database…
0
votes
1 answer

In laravel how to seed existing databse using array?

I want to seed existing database in my application. here is db with data in it: I made array in seeder and now I want to assign those values to my fields with one loop I guess; This is migration: public function up() { …
Nika Simon
  • 45
  • 1
  • 7
0
votes
1 answer

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null?

I got this error when try to seed database. Laravel 7. BlogPost Model class BlogPost extends Model { protected $fillable = [ 'title', 'slug', 'user_id', 'category_id', 'excerpt', 'content_raw', …
serii
  • 67
  • 2
  • 9
0
votes
0 answers

php artisan db:seed no error no output not seeding

I have php 8.1.0 and laravel 8 every other thing seems to work and run just fine, except for php artisan db:seed when running it gives me no output, not any errors just not seeding. This problem started when I upgraded my pc and wanted to set up…
Abdullah Qasemi
  • 449
  • 1
  • 12
0
votes
0 answers

php artisan migrate:fresh --seed, send and error

My Model class ProfilerContract extends Model { protected $table = 'profiler_contracts'; protected $attributes = [ 'contract_type', 'contract_description', 'profiler_infos_id', 'created_at', 'updated_at', ]; protected $fillable…
0
votes
1 answer

Laravel Factory : How to make one field have content and the other one be null

I am creating a tag factory, and I want to it to either generate a project_tag_id or a gobal_tag_id but not both return [ 'project_card_id' => ProjectCard::inRandomOrder()->first()->id, 'user_id' => User::inRandomOrder()->first()->id, …
Jakub
  • 1,260
  • 1
  • 13
  • 40
0
votes
1 answer

Too few arguments to function Database\Seeders\DatabaseSeeder::Database\Seeders\{closure}()

I want to test my database and I have made factories and the following is my code inside my seeder file public function run() { Category::factory()->count(10)->create(); User::factory() ->has(Profile::factory()->state(function…
Bilal Yousaf
  • 61
  • 1
  • 1
  • 7
0
votes
1 answer

When I ran seed command, I got error 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected'

I started new Laravel project development. I set .env file and database.php file, migration and seeder file. I got success in migration but failed in seeding. I got the following error when I ran php artisan db:seed. SQLSTATE[3D000]: Invalid…
blue pine
  • 472
  • 6
  • 17
0
votes
1 answer

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax error when seeding data to db

I am getting this error when I try to seed data into the database. I have the bookings table and a bookings package table, seeder, factory and models, and also a pivot table that links them. here is my bookingpackage seeder