Wednesday, March 29, 2017

Try catch not working in Laravel 5.4

Started working in Laravel 5.4 recently. I tried to catch an error and return an json response from controller in following code but it seems throwing error usual

try{
            $credentials = $request->only('Username', 'Password');
            $driverdata = Driver::where('username',$credentials['Username'])
                        ->where('password',Hasher::hash($credentials['Password']))
                        ->where('deleted',0)
                        ->where('blocked',0)
                        ->where('status',1)
                        ->first(); 
            // if(!count($driverdata)){
            //  return response()->json("Login Failed",401);
            // }                        
            return response()->json($driverdata->toArray());
        } 
        catch(Exception $e){
           return response()->json($e->getMessage(),500);
        }

I also tried

use Exception;

on top also using as

catch(\Exception $e){
    return response()->json($e->getMessage(),500);
}

Following is the error i get

FatalErrorException in LoginController.php line 26:
Call to a member function toArray() on null

Any suggestions? But the same thing used to work in Laravel 5.2



via Karthik Nk

Advertisement