Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call database seeder from a subfolder

Tags:

php

laravel-4

I want to create a set of database seed classes specifically for adding data for test cases I'm writing.

My plan was to put them in the folder:

app/database/seeds/testData/

and then call the seeder via the command:

php artisan db:seed --class="testData/myTestSeeder"

But I get a "class does not exist" error.

Is it possible to call database seeders that live in a subfolder in seeds? I don't see an explicit "yes" in the docs, but I don't see an explicit "no" either.

like image 435
Chris Schmitz Avatar asked May 28 '14 19:05

Chris Schmitz


People also ask

Which command is used to seed your database?

The db:seed command is used to add records to a database automatically using a Seeder ( Illuminate\Database\Seeder ) class to generate or provide the records.

What are seeders in database?

A seeder is a special class used to generate and insert sample data (seeds) in a database.


2 Answers

You shouldn't need to edit your classmap on your project, just make sure to run

composer dump-autoload

after moving your class to a subfolder.

Once you've done that, run this (no need to mention testData here)

php artisan db:seed --class="myTestSeeder"
like image 195
Maury Avatar answered Sep 22 '22 16:09

Maury


You need to tell the autoloader how to load your new class. This is relatively simple; add the following to your composer.json in the classmap property:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/database/seeds/testData"  // <-- Add this
    ]
},

After that, run composer dump-autoload and your seed file should now be loaded successfully.

like image 38
Jeff Lambert Avatar answered Sep 21 '22 16:09

Jeff Lambert