php - Laravel - Localhost return array but server return object

398

I have same code running in localhost + server, but the response from Laravel is different.

In localhost, I get anarray. In server, I get anobject.

I don't know why, because this never happen.

I'm using Laravel 5.7.x and the defaultresponse()->json(...) method.


CODE

LOCALHOST (Windows 10, php 7.3.14)

  "departments": [
    {
      "name": "Sem departamento",
      "vacancies": [
        {
          "id": 264,
          "title": "OFICIAL DE MANUTENÇÃO PREDIAL (REFRIGERAÇÃO)",
          "pcd": 0,
          "internal_code": null,
          "company_department_id": 0,
          "exam_filename": null,

Server (DigitalOcean) (Ubuntu 18.04, PHP 7.3.11)

  "departments": {
    "0": {
      "name": "Sem departamento",
      "vacancies": [
        {
          "id": 74,
          "title": "Técnico de Sistemas Júnior",
          "pcd": 0,
          "internal_code": null,
          "company_department_id": 0,
          "exam_filename": null,

Script

        $departments[0] = [
            'name' => 'Sem departamento',
            'vacancies' => []
        ];
        foreach ($vacancies as $v) {
            if (empty($v->companyDepartment)) {
                $departments[0]['vacancies'][] = $v;
            } elseif (empty($departments[$v->companyDepartment->id])) {
                $departments[$v->companyDepartment->id] = [
                    'name' => $v->companyDepartment->name,
                    'vacancies' => [$v]
                ];
            } else {
                $departments[$v->companyDepartment->id]['vacancies'][] = $v;
            }
        }

        return response()->json(compact('departments'));

People are also looking for solutions to the problem: php - codeigniter .htaccess in shared hosting (mochahost)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.