簡體   English   中英

Laravel localhost有效,但heroku給出了500錯誤

[英]Laravel localhost works, but heroku gives 500 error

我正在一個需要將一些數據從excel轉換為數據庫的項目中。 這在帶有laravel的本地主機上工作正常,但是當進入heroku時,它會給我500個內部服務器錯誤。

我在heroku中搜索了可能的內容,然后我發現heroku的數據庫有2個值從請求中插入。 所以這意味着代碼循環兩次,但隨后發生錯誤。

我試圖在代碼中找到錯誤4小時,但我沒有發現發生了什么......

這是代碼:

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;

    }

使用以下命令在heroku中設置2個配置變量:

1 heroku config:set APP_DEBUG=true
2 heroku config:set APP_KEY=RandomString

設置這些鍵后,您將能夠看到實際的錯誤,而不是一般的500。

對於heroku中的密鑰問題,請在config / app.php中添加以下代碼:

'key' => env('APP_KEY', 'SomeRandomStringSomeRandomString'),

在終端中寫入此命令

heroku config:set APP_KEY=SomeRandomStringSomeRandomString

在花了幾個小時試圖弄清楚代碼發生了什么之后,我發現這是Apache超時的問題。

我在這里找到了答案。

致謝: Eric Cope

我發現了為什么我有500錯誤。 這是因為我把日期配置錯了。

所以,這篇文章解決了我的問題。

在查找錯誤的研究期間,我發現laravel在部署時也有調試消息。 所以我將調試模式更改為true。

目錄:“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),

我還想補充說我也遇到過這個問題,但那是因為我忘了為Heroku應用程序添加APP_KEY

試試這個heroku config:set APP_DEBUG=true然后訪問你的應用程序,看看究竟是什么錯誤。 很可能是數據庫連接失敗或缺少.env鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM