public function csv() { if (isset($_FILES['csv'])) { ini_set('max_execution_time', 300); $csv = Input::file('csv'); $csv->move(base_path() . '/../csv/', 'alunos.csv'); if ($csv->getClientOriginalExtension() !== 'csv') { return Redirect::to('/aluno/csv')->withDanger('O arquivo deve ter a extensão .csv'); } $csv = utf8_encode(file_get_contents(base_path() . '/../csv/alunos.csv')); $csv = explode("\n", $csv); array_shift($csv); array_pop($csv); foreach ($csv as $key => $row) { $row = rtrim($row, ';'); $row = explode(';', $row); $matricula = $row[0]; $nome = $row[1]; $curso = $row[2]; $turma = $row[3]; $tipo_curso = rtrim($row[4]); // $data_inicio = $row[5] !== '' ? $row[5] : date('Y-m-d'); $data_inicio = date('Y-m-d H:i:s'); $modelCurso = Curso::whereNome($curso)->first(); if ($modelCurso === null) { $modelCurso = new Curso(); $modelCurso->nome = $curso; $modelCurso->tipo = $tipo_curso; $modelCurso->save(); } $modelTurma = Turma::whereSigla($turma)->first(); if ($modelTurma == null) { $modelTurma = new Turma(); $modelTurma->sigla = $turma; $modelTurma->descricao = 'Turma do curso de' . $modelCurso->nome; $modelTurma->data_inicio = $data_inicio; $modelTurma->curso_id = $modelCurso->id; $modelTurma->save(); } $aluno = Aluno::find($matricula); if ($aluno == null) { $aluno = new Aluno(); $aluno->matricula = $matricula; $aluno->nome = $nome; $aluno->turma_id = $modelTurma->id; $aluno->foto = 'placeholder.jpg'; $aluno->save(); } } return Redirect::to('/aluno')->withSuccess('Alunos registrados com sucesso!'); die; } return View::make('aluno.csv'); }