【问题标题】:Vue - Laravel - SPA 500 (Internal Server Error)Vue - Laravel - SPA 500(内部服务器错误)
【发布时间】:2020-03-02 08:41:20
【问题描述】:

我不知道为什么会出现这个错误。

检查网络选项卡后,它只显示“消息:”服务器错误”

这仅在更改路线和刷新页面时随机发生。有时它不会出现,有时它会出现。它也发生在我的另一个 apiget 函数上。

routes.js

  { 
    path: '/', 
    redirect: '/dashboard',
    component: () => import('@/views/BaseView'),
    meta: { requiresAuth: true },
    children: [
      { 
        path: '/dashboard', 
        name: 'dashboard', 
        component: () => import('@/views/Dashboard'),
      },
      { 
        path: 'objective/employee/:id', 
        name: 'employee-objective', 
        component: () => import('@/views/employee-objective/index'),
      },
    ]
  },

api

Route::middleware('auth:api')->group(function () {
  Route::get('user', function (Request $request) {
    return $request->user();
  });
  Route::get('employee-objectives', 'EmployeeObjectiveController@objectives');
});

laravel 控制器

  public function objectives(Request $request) {
    $get = EmployeeObjective::with('corporateObjective:id,corporate_objective,corporate_objective_description', 
    'employeeObjectiveKpa:id,employee_objective_id,department_objective_id,kpa_info,kpi_info,kpi_progress,kpa_weight,kpa_score_1,kpa_equal,created_at')
    ->where('employee_id', $request->employee_id)
    ->first();
    return response()->json($get);
  }

component.vue

 created() {
      setTimeout(() => {
        this.$store.state.loading = false;
      },1000);
      this.getEmployeeObjectiveKPA()
    },

 methods: {
      getEmployeeObjectiveKPA() {
        axios.get('/api/employee-objectives', { 
          params: { employee_id: this.$store.state.authUser.employee_id } 
        })
        .then(response => {
          if (response.data.employee_objective_kpa !== undefined) {
            this.employee_objective_kpa = response.data.employee_objective_kpa
          }
        })
        .catch(error => console.log(error))
      },

welcome.blade.php

 <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">

        <title>Appraisal</title>

        <meta name="csrf-token" content="{{ csrf_token() }}">
    <script>window.Laravel = { csrfToken: '{{ csrf_token() }}' }</script>

  </head>

storage/logs/laravel(我把它删减了,这是 #35 行错误)

[2020-03-02 19:59:44] production.ERROR: No application encryption key has been specified. {"exception":"[object] (RuntimeException(code: 0): No application encryption key has been specified. at C:\\laragon\\www\\appraisal\\vendor\\laravel\\framework\\src\\Illuminate\\Encryption\\EncryptionServiceProvider.php:44)
[stacktrace]
#0 C:\\laragon\\www\\appraisal\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\helpers.php(422): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\{closure}(NULL)
#1 C:\\laragon\\www\\appraisal\\vendor\\laravel\\framework\\src\\Illuminate\\Encryption\\EncryptionServiceProvider.php(48): tap(NULL, Object(Closure))

【问题讨论】:

  • 您是否尝试过检查服务器端日志以查看是否抛出了任何异常?我会在您的 .env 文件中启用调试模式,以便您可以看到错误。
  • 我的.env 已经设置为APP_DEBUG=true,但我不确定如何使用它。
  • 您能否提供更多信息,例如您的 Laravel 和 Vue 路由是如何设置的?
  • 你好@tamrat 请看我编辑的帖子。
  • 问题似乎在 laravel 方面,可能是重新检查查询执行?请查看laravel.com/docs/4.2/errors#logging

标签: laravel vue.js


【解决方案1】:

您是如何获得此查询的?您想做什么? with() 方法是急切地加载模型关系。另外,中间的', ' 是完全无效的。因为它应该是一个关系数组或一个关系的单个字符串。

$get = EmployeeObjective::with('corporateObjective:id,corporate_objective,corporate_objective_description', 
'employeeObjectiveKpa:id,employee_objective_id,department_objective_id,kpa_info,kpi_info,kpi_progress,kpa_weight,kpa_score_1,kpa_equal,created_at')

这些只是 EmployeeObjective 的属性吗?为此使用select()。还有,里面有冒号,为什么?

另外,如果我是正确的,您可以立即返回查询结果。无需调用 response 方法,因为它会在返回时自动将您的结果 JSON 化。

【讨论】:

  • with('relationship:column', relationship:column)的使用,是关系模型的属性/列。我只是想加载获取数据所需的列,从而降低它的大小。减少了大约 400kb。 (虽然我不确定它是否真的会影响一般用户的加载)。哦,我现在要删除 json,谢谢。
  • 啊,我明白了,那里的文档有点误读。无论如何,也许将它包装在一个数组中。像这样:with(['corporateObjective:id,corporate_objective,corporate_objective_description', 'employeeObjectiveKpa:id,employee_objective_id,department_objective_id,kpa_info,kpi_info,kpi_progress,kpa_weight,kpa_score_1,kpa_equal,created_at']) 如果仍然无法正常工作,请添加您的 EmployeeObjective 模型代码 :)
  • 哦,你是否包括了关系的 id 和外键?根据文档:When using this feature, you should always include the id column and any relevant foreign key columns in the list of columns you wish to retrieve.
  • 是的,我已经设法包含了关系模型的idforeign key。在with() 中添加[ ] 之后,不知何故现在很少出现错误,尽管它可能是巧合(就像我说的那样,这个错误只是不时突然出现)。谢谢你。我仍然会尝试更多次刷新我的组件。
  • 哦,这真的很容易解决。您没有用于 cookie 加密的加密密钥集。请在您的根目录中运行php artisan key:generate。这将在您的.env 文件中设置APP_KEY 值,应该可以解决问题!
【解决方案2】:

我认为这可以解决所有问题。 php artisan config:cache

来源:https://github.com/laravel/framework/issues/25964

谁能解释一下这个解决方案?

【讨论】:

    猜你喜欢
    • 2019-01-24
    • 2020-05-09
    • 2018-10-16
    • 2014-10-25
    • 2015-08-12
    • 1970-01-01
    • 2017-04-12
    • 2018-08-01
    • 2020-10-01
    相关资源
    最近更新 更多