php – Laravel can't call array key from requests-ThrowExceptions

Exception or error:

This is my code

<?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;
      }
    }
}

and it gives me this error:

"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": "->"
    }
]
}
How to solve:

No need to loop the $response. You can just do

$email = $response['name'];

or directly on request instance

$email = $request->email;

Instead of writing your custom validation logic you could use Laravels validation. Something like:

public function login(Request $request) {

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

   ...
}

or just check if the values are present in the request

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

Update to answer author question in the comment

Let’s say you dd($response) and you get something like:

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

When you foreach this

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
}

Leave a Reply

Your email address will not be published. Required fields are marked *