如何在laravel中将csv导入数据库

时间:2019-05-19 11:30:52

标签: laravel oracle forms csv import

我需要您的宝贵帮助/建议!我已经在Laravel中创建了一个表单。用户插入一些数据,然后提交,他/她选择了一个.csv文件进行上传。我希望将该文件插入现有的(oracle)数据库表中,以运行查询并将结果导出为pdf。但是我得到了未定义的偏移量:8错误

我已经在cmd运行php artisan make:migration create_csv_data_table,并创建了表字段到database \ migrations csv_data_table。我已经将此迁移到数据库(使用php artisan migration),并将表csv_data创建到我的oracle数据库中。我还创建了对应模型(使用php artisan make:model Csvdata)。在下面您可以看到我的代码:

1)这是我的web.php摘录

Route::get('/diploma', 'PdfController@quest');
Route::post('/hard', 'PdfController@job');

Route::get('/p', 'PdfController@pointer');
Route::get('/export1','PdfController@export1')->name('export1.pdf');

2)这是我在其中输入数据的表单:

    <form method='post' enctype="multipart/form-data" action="/hard">

        {{csrf_field()}}

        <section>
        <br>
            <legend><i><b> Συμπλήρωση Στοιχείων Παραρτήματος Διπλώματος</b></i></legend>
        <br>
        </section>

        <section>   
        Επιλογή Προγράμματος Σπουδών:
            <select name="sc" id="xaos">

            <optgroup label="postgraduates">
                @foreach($transport as $y)
                    <option value="{{$y->object_id}}">{{$y->object_name}}</option>
                @endforeach
            </optgroup>

            </select>
        </section>

    <br>

        <section>
        Αριθμός Μητρώου Φοιτητή: 
            <input name='am' type='number' min="1000000" max="1999999" required="" oninvalid="this.setCustomValidity('1000000 < Τιμή < 1999999')">
        </section>

        <br>

        <section>
        Επιλογή Γλώσσας:
            <select name="language" id="lang">
                    <option value="GR"> Greek</option>
                    <option value="EN"> English</option>
            </select>
        </section>

        <br>

        <section>
        <label for="upload-file">select csv file</label>
            <input type="file" name="upload-file" class="form-control">
        </div>
        <input class="btn btn-success" type="submit" value="Upload " name="submit">

        <section>
        <br>
            <input type='submit' value="Submit!">
        </section>

    </form>

3)这是迁移文件类:

class CreateCsvDataTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('csv_data', function (Blueprint $table) 
        {
            $table->integer('ac1')-> nullable();
            $table->string('ac2')-> nullable();
            $table->string('ac3')-> nullable();
            $table->integer('ac4')-> nullable();
            $table->integer('ac5')-> nullable();
            $table->string('ac6')-> nullable();
            $table->integer('ac7')-> nullable();
            $table->string('ac8')-> nullable();
            $table->string('ac9')-> nullable();
            $table->integer('ac10')-> nullable();
            $table->integer('ac11')-> nullable();
            $table->integer('ac12')-> nullable();
            $table->integer('ac13')-> nullable();
            $table->float('ac14')-> nullable();
            $table->integer('ac15')-> nullable();
            $table->integer('ac16')-> nullable();
            $table->integer('ac17')-> nullable();
            $table->string('ac18')-> nullable();
            $table->string('ac19')-> nullable();
            $table->string('ac20')-> nullable();
            $table->string('ac21')-> nullable();
            $table->integer('ac22')-> nullable();
            $table->float('ac23')-> nullable();
            $table->string('ac24')-> nullable();
            $table->float('ac25')-> nullable();
            $table->float('ac26')-> nullable();
            $table->string('ac27')-> nullable();
            $table->float('ac28')-> nullable();
            $table->string('ac29')-> nullable();
            $table->string('ac30')-> nullable();
            $table->string('ac31')-> nullable();
            $table->string('ac32')-> nullable();
            $table->integer('ac33')-> nullable();
            $table->string('ac34')-> nullable();
            $table->string('ac35')-> nullable();
            $table->string('ac36')-> nullable();
            $table->string('ac37')-> nullable();
            $table->string('ac38')-> nullable();
            $table->string('ac39')-> nullable();
            $table->string('ac40')-> nullable();
            $table->string('ac41')-> nullable();
            $table->integer('ac42')-> nullable();
            $table->date('ac43')-> nullable();
            $table->string('ac44')-> nullable();
            $table->string('ac45')-> nullable();
            $table->string('ac46')-> nullable();
            $table->string('ac47')-> nullable();
            $table->string('ac48')-> nullable();
            $table->string('ac49')-> nullable();
            $table->string('ac50')-> nullable();
            $table->string('ac51')-> nullable();
            $table->string('ac52')-> nullable();
            $table->string('ac53')-> nullable();
            $table->string('ac54')-> nullable();
            $table->integer('ac55')-> nullable();
            $table->integer('ac56')-> nullable();
            $table->integer('ac57')-> nullable();
            $table->integer('ac58')-> nullable();
            $table->integer('ac59')-> nullable();
            $table->string('ac60')-> nullable();
            $table->string('ac61')-> nullable();
            $table->string('ac62')-> nullable();
            $table->integer('ac63')-> nullable();
            $table->date('ac64')-> nullable();
            $table->date('ac65')-> nullable();
            $table->date('ac66')-> nullable();
            $table->string('ac67')-> nullable();
            $table->integer('ac68')-> nullable();
            $table->string('ac69')-> nullable();
            $table->integer('ac70')-> nullable();
            $table->integer('ac71')-> nullable();
            $table->string('ac72')-> nullable();
            $table->string('ac73')-> nullable();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('csv_data');
    }
}

4)这是控制器节选(当在我的表单上按Upload时,我得到未定义的偏移量:8错误并显示$ csv_data-> ac9 = $ data [8];已标记)

public function job(Request $p)
    {
        $a1 = $p -> get('sc');  //όνομα προγράμματος
        $a2 = $p -> get('am');  //αριθμός μητρώου φοιτητή
        $a3 = $p -> get('language');  //γλώσσα

        if (($handle = fopen ( 'MOCK_DATA.csv', 'r' )) !== FALSE) 
        {
            while ( ($data = fgetcsv ( $handle, 1000, ',' )) !== FALSE )
                {
                    {
                        $csv_data = new Csvdata ();
                        $csv_data->ac1  =$data [0];
                        $csv_data->ac2  =$data [1];
                        $csv_data->ac3  =$data [2];
                        $csv_data->ac4  =$data [3];
                        $csv_data->ac5  =$data [4];
                        $csv_data->ac6  =$data [5];
                        $csv_data->ac7  =$data [6];
                        $csv_data->ac8  =$data [7];
                        $csv_data->ac9  =$data [8];
                        $csv_data->ac10 =$data [9];
                        $csv_data->ac11 =$data [10];
                        $csv_data->ac12 =$data [11];
                        $csv_data->ac13 =$data [12];
                        $csv_data->ac14 =$data [13];
                        $csv_data->ac15 =$data [14];
                        $csv_data->ac16 =$data [15];
                        $csv_data->ac17 =$data [16];
                        $csv_data->ac18 =$data [17];
                        $csv_data->ac19 =$data [18];
                        $csv_data->ac20 =$data [19];
                        $csv_data->ac21 =$data [20];
                        $csv_data->ac22 =$data [21];
                        $csv_data->ac23 =$data [22];
                        $csv_data->ac24 =$data [23];
                        $csv_data->ac25 =$data [24];
                        $csv_data->ac26 =$data [25];
                        $csv_data->ac27 =$data [26];
                        $csv_data->ac28 =$data [27];
                        $csv_data->ac29 =$data [28];
                        $csv_data->ac30 =$data [29];
                        $csv_data->ac31 =$data [30];
                        $csv_data->ac32 =$data [31];
                        $csv_data->ac33 =$data [32];
                        $csv_data->ac34 =$data [33];
                        $csv_data->ac35 =$data [34];
                        $csv_data->ac36 =$data [35];
                        $csv_data->ac37 =$data [36];
                        $csv_data->ac38 =$data [37];
                        $csv_data->ac39 =$data [38];
                        $csv_data->ac40 =$data [39];
                        $csv_data->ac41 =$data [40];
                        $csv_data->ac42 =$data [41];
                        $csv_data->ac43 =$data [42];
                        $csv_data->ac44 =$data [43];
                        $csv_data->ac45 =$data [44];
                        $csv_data->ac46 =$data [45];
                        $csv_data->ac47 =$data [46];
                        $csv_data->ac48 =$data [47];
                        $csv_data->ac49 =$data [48];
                        $csv_data->ac50 =$data [49];
                        $csv_data->ac51 =$data [50];
                        $csv_data->ac52 =$data [51];
                        $csv_data->ac53 =$data [52];
                        $csv_data->ac54 =$data [53];
                        $csv_data->ac55 =$data [54];
                        $csv_data->ac56 =$data [55];
                        $csv_data->ac57 =$data [56];
                        $csv_data->ac58 =$data [57];
                        $csv_data->ac59 =$data [58];
                        $csv_data->ac60 =$data [59];
                        $csv_data->ac61 =$data [60];
                        $csv_data->ac62 =$data [61];
                        $csv_data->ac63 =$data [62];
                        $csv_data->ac64 =$data [63];
                        $csv_data->ac65 =$data [64];
                        $csv_data->ac66 =$data [65];
                        $csv_data->ac67 =$data [66];
                        $csv_data->ac68 =$data [67];
                        $csv_data->ac69 =$data [68];
                        $csv_data->ac70 =$data [69];
                        $csv_data->ac71 =$data [70];
                        $csv_data->ac72 =$data [71];
                        $csv_data->ac73 =$data [72];

                        $csv_data->save ();
                    }
                    fclose ( $handle );
                }
                $finalData = $csv_data::all ();
                dd($finalData);

0 个答案:

没有答案