This may sound a very trivial question but really I do not get how to populate mySQL database.
Yes, there are many many tutorials out there showing how to insert manually data into table, or how to construct a form to make the user insert his/her data.
My data are actually stored in many variables (array, lists...) and I need to put each one of this array in a column in my table.
Why I'm not able to find any example of how to insert existing data from variables into database? Should I first save all this data to a file(excel, csv)? Am I doing something wrong in the way I think a database should be populated? What is the best procedure/the normal proceeding to add existing data to a database table?
@36864 thanks a lot!
I'm trying to use seeder now....just figuring out where to put my code and from where can read variables...as you said there are few tutorials about this, maybe it is just too simple and I'm not seeing it.
I've recently migrated away from my WordPress blog to one I've built myself using Laravel. For this, I've used seeders to copy the database:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// And then the WordPress posts and comments
DB::unprepared(File::get(base_path() . '/database/seeds/wpposts.sql'));
DB::unprepared(File::get(base_path() . '/database/seeds/wpcomments.sql'));
DB::unprepared(File::get(base_path() . '/database/seeds/wpterms.sql'));
DB::unprepared(File::get(base_path() . '/database/seeds/wptags.sql'));
DB::unprepared(File::get(base_path() . '/database/seeds/wppostterms.sql'));
}
}
The SQL files are just plain SQLdumps/exports from the WP-database, retrieved using PHPMyAdmin, added to the database folder and called by running
@Thyrosis The problem with having production data in seeders, is if you ever run the seeder again(php artisan migrate --seed), you'll reinsert all of that data again causing duplicates. I like putting production data in the actual migration files just after Schema::create(), so it will only ever run once when the migration is run.
I can see @Cronix 's point. What's more there is another problem, I guess. Rollback a particular migration or create a new one might deprecate your seed data. For example, if you rollback a new table migration then your seed logic that populates this table will fail. Another example would be if you modify a table throught a new migration and you don't update your seed file. That would might fail, too. Generally, you must have your "migrations", "current data in your database" and "seed data" syncronized all the time.
I use the @Cronix 's method but I usually split up the migration into two different migration files. The first one which defines the table schema and the second one to populated it (with a down method which deletes this default data) thus you can rollback only your data and keep your table's schema.
I usually just put them in the migration file though
public $tableName = 'tableName';
public function up()
{
// create the table
Schema::create($this->tableName, function (Blueprint $table) {
$table->increments('id');
// other fields
});
// seed it
DB::unprepared(file_get_contents(base_path('database/migrations/seeds/thisMigration.sql')));
}
public function down()
{
Schema::dropIfExists($this->tableName);
}
@jurios
As said above, integrating data using a source (sql, json, or csv) should be much faster than doing it using the Querybuilder, if you have a lot of data...