Laravel не может вызвать ключ массива из запросов - PullRequest
0 голосов
/ 17 марта 2020

Это мой код

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;


class LoginController extends Controller
{
    public function login(Request $request) {
        $response = $request->all();
        print_r($response);
        $email = '';
        $password = '';
        foreach($response as $item) {
          $email = $item['email']
        }
        if($this->checkIfValid($email, $password) == true)  {
          $user = DB::Select('select * from user where email = ?', ['email' => $email]);
          var_dump($user);
        } else {
          return response()->json(['error' => 'Cant validate email and/or password']);
        }
    }

    private function checkIfValid($email, $password)  {
      if($email != '' && $password != '')  {
          echo('test');
          if(filter_var($email, FILTER_VALIDATE_EMAIL) == True) {
            echo('test2');
            return true;
          }
      } else {
        return false;
      }
    }

    private function checkEmailExists($email, $dbemail) {
      if($email == $dbemail)  {
        return true;
      } else {
        return false;
      }
    }
}

, и он дает мне эту ошибку:

"message": "Illegal string offset 'email'",
"exception": "ErrorException",
"file": "C:\\Users\\Home PC\\website\\app\\app\\Http\\Controllers\\LoginController.php",
"line": 18,
"trace": [
    {
        "file": "C:\\Users\\Home PC\\website\\app\\app\\Http\\Controllers\\LoginController.php",
        "line": 18,
        "function": "handleError",
        "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
        "type": "->"
    },
    {
        "function": "login",
        "class": "App\\Http\\Controllers\\LoginController",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php",
        "line": 54,
        "function": "call_user_func_array"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php",
        "line": 45,
        "function": "callAction",
        "class": "Illuminate\\Routing\\Controller",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php",
        "line": 225,
        "function": "dispatch",
        "class": "Illuminate\\Routing\\ControllerDispatcher",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php",
        "line": 182,
        "function": "runController",
        "class": "Illuminate\\Routing\\Route",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 681,
        "function": "run",
        "class": "Illuminate\\Routing\\Route",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 128,
        "function": "Illuminate\\Routing\\{closure}",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\SubstituteBindings.php",
        "line": 41,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php",
        "line": 76,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php",
        "line": 49,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\View\\Middleware\\ShareErrorsFromSession",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php",
        "line": 56,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Session\\Middleware\\StartSession",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php",
        "line": 37,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php",
        "line": 66,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Cookie\\Middleware\\EncryptCookies",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 103,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 683,
        "function": "then",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 658,
        "function": "runRouteWithinStack",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 624,
        "function": "runRoute",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 613,
        "function": "dispatchToRoute",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
        "line": 165,
        "function": "dispatch",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 128,
        "function": "Illuminate\\Foundation\\Http\\{closure}",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php",
        "line": 27,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php",
        "line": 63,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\fruitcake\\laravel-cors\\src\\HandleCors.php",
        "line": 36,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fruitcake\\Cors\\HandleCors",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\fideloper\\proxy\\src\\TrustProxies.php",
        "line": 57,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fideloper\\Proxy\\TrustProxies",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 103,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
        "line": 140,
        "function": "then",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
        "line": 109,
        "function": "sendRequestThroughRouter",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
    },
    {
        "file": "C:\\Users\\Home PC\\website\\app\\public\\index.php",
        "line": 55,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
    }
]
}

1 Ответ

0 голосов
/ 17 марта 2020

Не нужно l oop ответ $. Вы можете просто сделать

$email = $response['name'];

или непосредственно по запросу экземпляра

$email = $request->email;

Вместо того, чтобы писать свои собственные логи проверки c, вы можете использовать проверку Laravels. Что-то вроде:

public function login(Request $request) {

   $request->validate([
        'email'    => 'required|string|email',
        'password' => 'required|string'
   ]);

   ...
}

или просто проверьте, присутствуют ли значения в запросе

if ($request->has(['email', 'password'])) {
    ...
}

Обновление, чтобы ответить на вопрос автора в комментарии

Допустим, вы dd($response), и вы получите что-то вроде:

array:3 [▼
  "_token" => "r3hr..."
  "email" => "john.doe@gmail.com"
  "password" => "1234"
]

Когда вы проповедуете это

foreach($response as $item) {
    // $item here is the value 
    $email = $item['email']; // you can't do this

    $email = $item; // this you could do but it would overwrite each time
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...