I'm working in a project that needs to convert some data from excel to database. This is working fine in local host with laravel, but when goes to heroku, it gives me a 500 internal server error.
I searched in heroku for something that point what could be, then I discovered that the heroku's database has 2 values inserted from the requests. So it means that the code loops twice, but then the error occurs.
I tried to find the error in the code for 4 hours, and I didn't found what is going on...
Here is the code:
public function insertFromExcel(){ $excel = new \Maatwebsite\Excel\Facades\Excel(); $data = $excel::load('../../../excel_files/relacao_5.xls', function ($reader) { })->get(); foreach ($data as $row) { //-----------------------------------------Verifica Setor------------------------------------- if(isset($row['nome_setor'])){ $setor_id = DB::table('setor')->where('nome', '=', $row['nome_setor'])->pluck('id'); if ($setor_id == null) { $setor_id = DB::table('setor')->insertGetId([ 'nome' => $row['nome_setor'] ]); } } else{ $setor_id = null; } //-----------------------------------------Verifica Funcionario-------------------------------- $funcionario_id = DB::table('funcionario')->where('matricula', '=', $row['codfun'])->pluck('id'); if ($funcionario_id === null) { $funcionario_id = DB::table('funcionario')->insertGetId([ 'nome' => $row['nome_func'], 'matricula' => $row['codfun'], 'pis_pasep' => $row['pis_pasep'], 'data_admisao' => $row['admissao'], 'setor_id' => $setor_id ]); } else{ $funcionario_pis_pasep = DB::table('funcionario')->where('matricula', '=', $row['codfun'])->pluck('pis_pasep'); if($funcionario_pis_pasep == null || $funcionario_pis_pasep == 0){ DB::table('funcionario') ->where('id', $funcionario_id) ->update([ 'pis_pasep' => $row['pis_pasep'] ]); } $funcionario_setor = DB::table('funcionario')->where('matricula', '=', $row['codfun'])->pluck('setor_id'); if($funcionario_setor == null){ DB::table('funcionario') ->where('id', $funcionario_id) ->update([ 'setor_id' => $setor_id ]); } } //-----------------------------------------Verifica Cargos-------------------------------- if (strpos($row['descrnivcarg'], "GERENTE") !== false) { $gerente = DB::table('gerente') ->join('funcionario', 'gerente.funcionario_id', '=', 'funcionario.id') ->where('funcionario.id', '=', $funcionario_id) ->pluck('gerente.id'); if ($gerente == null) { $user_id = DB::table('usuario')->insertGetId(['senha' => '12345', 'nivel' => 3]); DB::table('gerente')->insert([ 'funcionario_id' => $funcionario_id, 'usuario_id' => $user_id ]); } } if (strpos($row['descrnivcarg'], "COORDENADOR") !== false) { $coordenador = DB::table('coordenador') ->join('funcionario', 'coordenador.funcionario_id', '=', 'funcionario.id') ->where('funcionario.id', '=', $funcionario_id) ->pluck('coordenador.id'); if ($coordenador == null) { $user_id = DB::table('usuario')->insertGetId(['senha' => '12345']); DB::table('coordenador')->insert([ 'funcionario_id' => $funcionario_id, 'usuario_id' => $user_id ]); } } if (strpos($row['descrnivcarg'], "SUPERVISOR") !== false) { $supervisor = DB::table('supervisor') ->join('funcionario', 'supervisor.funcionario_id', '=', 'funcionario.id') ->where('funcionario.id', '=', $funcionario_id) ->pluck('supervisor.id'); if ($supervisor == null) { $user_id = DB::table('usuario')->insertGetId(['senha' => '12345', 'nivel' => 2]); DB::table('supervisor')->insert([ 'funcionario_id' => $funcionario_id, 'usuario_id' => $user_id ]); } } if (strpos($row['descrnivcarg'], "ESTAGIARIO") !== false) { $estagiario = DB::table('estagiario') ->join('funcionario', 'estagiario.funcionario_id', '=', 'funcionario.id') ->where('funcionario.id', '=', $funcionario_id) ->pluck('estagiario.id'); if ($estagiario == null) { $user_id = DB::table('usuario')->insertGetId(['senha' => '12345', 'nivel' => 1]); DB::table('estagiario')->insert([ 'funcionario_id' => $funcionario_id, 'usuario_id' => $user_id ]); } } else { $cargo_id = DB::table('cargo')->where('nome', '=', $row['descrnivcarg'])->pluck('id'); if ($cargo_id == null) { $cargo_id = DB::table('cargo')->insertGetId(['nome' => $row['descrnivcarg']]); } $operario = DB::table('operario') ->join('funcionario', 'operario.funcionario_id', '=', 'funcionario.id') ->where('operario.id', '=', $funcionario_id) ->pluck('operario.id'); if ($operario == null) { DB::table('operario')->insert([ 'funcionario_id' => $funcionario_id, 'cargo_id' => $cargo_id, ]); } } } $funcionario_db[] = DB::table('funcionario')->select('matricula', 'nome', 'data_admisao', 'pis_pasep')->get(); return $funcionario_db; } 6 Answers
Set 2 config variables in heroku with following commands:
1 heroku config:set APP_DEBUG=true
2 heroku config:set APP_KEY=RandomString
After setting these keys you will be able to see the actual error instead of general 500.
6For problem with key in heroku add this code in config/app.php:
'key' => env('APP_KEY', 'SomeRandomStringSomeRandomString'), In terminal write this command
heroku config:set APP_KEY=SomeRandomStringSomeRandomString Try this heroku config:set APP_DEBUG=true then visit your app and see what's exactly is the error. Most likely is database connection failing or missing .env key.
After some more hours trying to figure out what is going on with the code, I discovered that it was a problem with the Apache timeout.
I found the answer here.
Credits: Eric Cope
I discovered the why I had the 500 error. It was because I was putting date configuration wrong.
So, this post solved my problem.
During the study to find the error, I discoverd that laravel has a debug messages when deploy too. So I change the debug mode to true.
directory: "laravel/config/app.php"
/* |-------------------------------------------------------------------------- | Application Debug Mode |-------------------------------------------------------------------------- | | When your application is in debug mode, detailed error messages with | stack traces will be shown on every error that occurs within your | application. If disabled, a simple generic error page is shown. | */ 'debug' => env('APP_DEBUG', true), On your heroku dahsboord, click on more, then run console then type php artisan key:generate Also, in your heroku settings, click on reveal config vars add APP_DEBUG true