0

I have this SQL data and I want to use it as seeder. Now my Team Lead said "That's a dump", additionally he said that I can use this sql data as SEEDER, but first I need to do something before I can use as seeder.

Can someone here knows what exactly what I should do first?

enter image description here

help-info.de
  • 6,695
  • 16
  • 39
  • 41

1 Answers1

1
  • Without Laravel: Depending on which MYSQL client you are using (PhpMyAdmin, Workbench, etc.), if you already have the table created in the database, you can just import the file to the client and the client will do the rest for you. Search how to import a dump file for your specific MySQL client.

  • With Laravel: What I do is

    • I copy the query itself (the part that starts with INSERT INTO) in another file in the storage folder (let's say in storage/app/country.sql)
    • In the seeder class now I get the content of the file which I insert with DB::insert
// database/seeds/CountrySeeder.php
class CountrySeeder extends Seeder
{
    public function run ()
    {
        $sql = file_get_contents(storage_path('app').'/country.sql');
        DB::insert($sql);
    }
}

Make sure the class is called in the DatabaseSeeder class

// database/seeds/DatabaseSeeder.php
class DatabaseSeeder extends Seeder
{
    public function run ()
    {
        $this->call([
            // ... 
            CountrySeeder::class,
            // ... other seeders
        ]);
    }
}
Prince Dorcis
  • 955
  • 7
  • 7
  • 1 C:\xampp\htdocs\geoloctask\geotask\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458 PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'geoloc.country' doesn't exist") 2 C:\xampp\htdocs\geoloctask\geotask\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458 PDO::prepare(" Still this error, thanks anyways :) – Joanna A. Rosalejos Aug 25 '20 at 03:47
  • The table `country` table does not exist in the database! Make sure you create it manually (via your MySQL client) or create a migration file for it. – Prince Dorcis Aug 25 '20 at 07:30
  • Are you importing the file directly to the database or you are seeding from Laravel? – Prince Dorcis Aug 25 '20 at 07:45