mercredi 30 mai 2018

Laravel seed table from multiple csv files

I'm very new to Laravel and Database and I'm trying to understand how to insert data into my database. Please be patient the question can sounds dummy for you.

  1. STEP

I created a table in migrations. Example of a table:

public function up(){

        Schema::create('job-urls', function (Blueprint $table) {
            $table->increments('id');
            $table->foreign('job_id')->references('id')->on('jobs');
            $table->string('url')->index();
            $table->string('hash');
            $table->timestamp('created_at')->nullable();
            $table->timestamp('updated_at')->nullable();

  1. STEP

I have two csv file that correspond to the field url and hash and I want to insert them. I created a new file in migration called populate_jobs_url

class PopulateJoburls extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(){
        $fileurls = fopen('../data/urls.csv', 'r');
        $filehash = fopen('../data/urls_hash.csv', 'r');

        while (($row = fgetcsv($fileurls, 0, ',')) !=FALSE){
            DB::table('joburls')->insert(
            array(
                'url' => $row,
            )
            );
        }
        while (($row = fgetcsv($filehash, 0, ',')) !=FALSE){
            DB::table('joburls')->insert(
            array(
                'hash' => $row,
            )
            );
            }
    }

Can you help me to understand how I check if the table is correctly filled? Is this approach correct? How could I insert data otherwise in my Database? Unfortunately all examples on the web deal with inserting manually data with a form.

Thanks



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IU6n7k
via IFTTT

Aucun commentaire:

Enregistrer un commentaire