3

I have a certain data that must be in table in order for my app to work, otherwise i get an error message.

For example if YOU or anyone else pulled my app from github and if you run php artisan migrate and then try to test the app you would get an error: data doesn't exist or something like that. and it's because there is no data in table.

So ideal solution would be after running:

 php artisan migrate

that you also get this needed data in that table.

This should be done somehow with the seeder but i don't know how. Can anyone help and give me an example?

How to make a seed for this data that should go into car_company table:

id car_company

1 Volkswagen

2 Mercedes

3 Audi

4 Porsche

there are 4 rows and i want to insert them after running

 php artisan db:seed
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
lewis4u
  • 14,256
  • 18
  • 107
  • 148
  • Ive used PHP app's that have this and its good. I've planned to write a script that does this myself. the way I would do it is simply add a check in the initialisation stage of the app. You could check what number of migration the source code is at, and then query the migration-tracking-table in the DB, if the DB is behind the code base you could stop the execution of the app and just print a message - *you need to run migrations* – the_velour_fog Aug 27 '16 at 12:04
  • yeah, i just want to make this seed after running the migration upon the very first time, so there is this data in table in order for app to be fully functional! – lewis4u Aug 27 '16 at 12:15
  • well I think the issue is more detection and displaying a message. its simple to create a shell alias or something which will simply do both commands, but then you need to educate people that they need to learn and locate and use your new script/command. But people already know you cant use a DB unless its seeded. and the artisan command to seed is already common knowledge. The other issue is when you automate something like seeding, you lose control of when the seeding happens, so the DB could get seeded when you didnt want it too. – the_velour_fog Aug 27 '16 at 12:28
  • Thats why I think either stopping the app execution and printing a message to the dev that DB is not updated properly, and possibly give a config option which lets them override the message and force use of an outdated DB if they want to ignore the messages. or maybe print to a log file. they problem with that is whether the log file will be watched or not. – the_velour_fog Aug 27 '16 at 12:31
  • yes, but you are going too deep into configuration my view of this problem is that you can't use the app if you don't have data in table so to solve that problem user just needs to run 2 commands: 1. php artisan migrate which is a normal thing and everyone knows it and 2. php artisan db:seed and i don't know how to make a seed for 10 new insertions in my table. that is the problem here, nothing else. – lewis4u Aug 27 '16 at 12:33

3 Answers3

7

At first run the following command :

php artisan make:seeder CarCompanyTableSeeder. This will create class CarCompanyTableSeeder in database\seeds.

enter image description here

Inside the function public function run() please add the following codes :

public function run()
    {
        $data = array(
            array(
                'id'            =>    1,
                'car_company'   =>   'Volkswagen'
            ),
            array(
                'id'            =>    2,
                'car_company'   =>   'Mercedes'
            ),
            array(
                'id'            =>    3,
                'car_company'   =>   'Audi'
            ),
            array(
                'id'            =>    4,
                'car_company'   =>   'Porsche'
            )
        );

        DB::table('car_company')->insert($data);
    }

There is another class class DatabaseSeeder extends Seeder in database\seeds. Inside the class there is a function run(). Add the following code there :

public function run()
    {
        Model::unguard();

        $this->call(CarCompanyTableSeeder::class);

        Model::reguard();
    }

Now, when you will run php artisan db:seed, then your desired values will be inserted there.

Rumel
  • 319
  • 1
  • 3
  • 19
  • 1
    @Rumel This is an example how all the answers should look like! – lewis4u Aug 27 '16 at 16:37
  • 1
    Thanks @lewis4u. I am very happy that my answered helped you. :) – Rumel Aug 27 '16 at 16:41
  • No offense (@Marcin Nabiałek and @Ray Cheng) you are probably more experienced and it is maybe a trivial thing for you to do this and maybe you don't have time to write it all like Rumel did. My opinion is that such an answer deserves respect and it will certainly help more than just writing where the answer can be found. It's easy to write: "Just google it!" but that is not the point of Stack Overflow...this is only my opinion and you don't have to agree upon it! – lewis4u Aug 27 '16 at 16:47
0

After run php artisan migrate you need run php artisan db:seed, to write seeds to database, you can follow this instructions: https://laravel.com/docs/5.2/seeding#using-model-factories

another useful package is: https://github.com/fzaninotto/Faker, help you fake datas to database.

Raymond Cheng
  • 2,425
  • 21
  • 34
  • fzaninottot/Faker creates dummy data and i want my data to be inserted. For example i have a table car_company and i want these fields to be seeded in my table after i run php artisan db:seed id car_company 1 Volkswagen 2 Mercedes 3 Audi 4 Porsche How should this seed look like? – lewis4u Aug 27 '16 at 13:13
  • @lewis4u, if you want exactly datas you should dump your datas to a xxx.sql file, then next time import it after migrate. Or write static datas in seed file. – Raymond Cheng Aug 27 '16 at 13:34
  • ok, i get this...i can export the data but how to import it with laravel. is that possible? or i must use DBMS or phpmyadmin? – lewis4u Aug 27 '16 at 13:44
  • @lewis4u, from shell command you can use this command `mysql -uroot (-ppasswd) databasename < sqlfilename.sql` to import to database, i think you can write a shell, then use php to excute the shell. – Raymond Cheng Aug 27 '16 at 13:47
  • ok that will do, but still...i would like to have a seed to fill in my data :( – lewis4u Aug 27 '16 at 13:51
  • @lewis4u, if this helpful , please take my answer, then we both get reputations.thx – Raymond Cheng Aug 27 '16 at 13:55
  • If you know how to write a seed for inserting more than one row in table please update your answer. – lewis4u Aug 27 '16 at 14:53
0

At the moment for such situations I use database migrations. You don't need to have in migrations only creating database structure, but you can also in migrations add data to database so it will be enought to run php artisan migrate to create database structure and create required data in database.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Please can you make such a migration example with inserting more than one row into the table? for example like in my question if i have a migration to create a table car_companys (with fields id and name) and i want to insert 2 rows: BMW and AUDI....how would that look like? – lewis4u Aug 27 '16 at 16:08