Laravel localhost 有效,但 heroku 给出 500 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31665706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel localhost works, but heroku gives 500 error
提问by Leo Ribeiro
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.
我正在一个需要将一些数据从 excel 转换为数据库的项目中工作。这在带有 Laravel 的本地主机中工作正常,但是当转到 heroku 时,它给了我 500 内部服务器错误。
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.
我在 heroku 中搜索了一些指向可能是什么的东西,然后我发现 heroku 的数据库有 2 个从请求中插入的值。所以这意味着代码循环了两次,但随后发生了错误。
I tried to find the error in the code for 4 hours, and I didn't found what is going on...
找了4个小时的代码中的错误,没有发现是怎么回事……
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;
}
回答by svikramjeet
Set 2 config variables in heroku with following commands:
使用以下命令在 heroku 中设置 2 个配置变量:
1 heroku config:set APP_DEBUG=true
2 heroku config:set APP_KEY=RandomString
1 heroku config:set APP_DEBUG=true
2heroku config:set APP_KEY=RandomString
After setting these keys you will be able to see the actual error instead of general 500.
设置这些键后,您将能够看到实际错误而不是一般的 500。
回答by Gabriele Lanzafame
For problem with key in heroku add this code in config/app.php:
对于 Heroku 中的 key 问题,在 config/app.php 中添加以下代码:
'key' => env('APP_KEY', 'SomeRandomStringSomeRandomString'),
In terminal write this command
在终端写这个命令
heroku config:set APP_KEY=SomeRandomStringSomeRandomString
回答by Salam
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.
试试这个,heroku config:set APP_DEBUG=true
然后访问您的应用程序,看看到底是什么错误。最有可能是数据库连接失败或丢失.env
密钥。
回答by Leo Ribeiro
回答by Ellie Strejlau
I also want to add that I encountered this as well, but it was because I forgot to add the APP_KEY
for the Heroku app.
我还想补充一点,我也遇到过这种情况,但这是因为我忘记APP_KEY
为 Heroku 应用程序添加。
回答by Leo Ribeiro
I discovered the why I had the 500 error. It was because I was putting date configuration wrong.
我发现了为什么我有 500 错误。那是因为我把日期配置错了。
So, this postsolved 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.
在研究发现错误的过程中,我发现laravel在部署时也有调试消息。所以我将调试模式更改为true。
directory: "laravel/config/app.php"
目录:“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),