dimanche 1 novembre 2015

Inserting excel data into database

I'm using Maatwebsite laravel-excel package to read my excel file.
And the code below shows how I'm trying to insert the data into my database.

First I post my file from my view:
{!! Form::open(array('url' => route('teste'))) !!}
    {!! Form::file('filename')!!}
    {!! Form::submit('Importar') !!}                
{!! Form::close() !!}

Then I use a route to redirect me to my Controller's method:
public function importData(Request $request){

    Excel::load($request->input('filename'), function($reader) {
       $reader->ignoreEmpty();
       $results = $reader->get()->toArray();
       foreach($results as $key => $value){
          $turma = new import_temp;
          $turma->cod_disciplina = $value['cod_disciplina'];
          $turma->nome_disciplina = $value['nome_disciplina'];
          $turma->cod_turma = $value['cod_turma'];  
           //....
            }
        })->get();
    }
}

I have three questions:

1) Is there a cleaner/better way to pass the name of the file ? As that's all I need to read it;
2) As you can see by my foreach, I have to write every single field of my table with the model instance $turma. Is there a better way to insert this kind of data?
3) Also, my IDE gives me a warning about the way I'm assign the data: model->field:

Referenced field is not found in subject class



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Q0COgF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire