3

I'm building a web application using Laravel 5. The tutorial I followed started with no data.

  1. Create migrate table.

    It uses:

    php artisan make:migration create_table-name_tables --create="table_name"
    
  2. Set up schema

    Then set up table schema in /database/migrations/_create_table-name_tables.php .

  3. Perform migration: php artisan migrate.

  4. Seeders.

    Create /database/seeds/table-nameTableSeeder.php. In this file there is actual rows in the table defined.

  5. Add seed class to /database/seeds/DatabaseSeeder.php:

    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    
    class DatabaseSeeder extends Seeder {
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
    
        $this->call('Table-nameTableSeeder');       
    }
    
    }
    
  6. Seed with:

    composer dump-autoload
    php artisan db:seed
    

    My question: If I already have a existing database. How do I modify migration steps to use my database? Do I just need to do step 1,2 and 3? Then I can move on to Creating models?

Thanks in advance!

2
  • Can i see your .env file? Commented Sep 27, 2015 at 22:41
  • Yes, I've modified the .env file according to my database. DB_HOST=localhost DB_DATABASE=my_database_name DB_USERNAME=root DB_PASSWORD=MYPASSWORD Commented Sep 28, 2015 at 0:06

1 Answer 1

2

According to the documentations:

Laravel includes a simple method of seeding your database with test data using seed classes.

That is to say, seeding your database is for testing purposes. So yes, if you already have an existing database with data in it, there is no need to seed it any further unless you need additional data.

Sign up to request clarification or add additional context in comments.

5 Comments

So I only need to do step 1, 2 and 3?
Yes. In fact, there is no need to seed your database unless you need the data.
One moe question, since I already have tables in my database, would php artisan migrate wipe out my existing data and leave a empty table?
That depends on your migration. If your migration is to simply modify the table, it won't be wiped. But if somewhere in your migration you drop then table, then it will.
I'm not even modify anything, so in step 2 do I just list all columns in /database/migrations/_create_table-name_tables.php ?

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.