Home » Php » php – i am getting error when i am fetching user data from database in laravel through API

php – i am getting error when i am fetching user data from database in laravel through API

Posted by: admin February 25, 2020 Leave a comment

Questions:

Hello Guys nowadays am working on laravel api everything is perfect just one issue am facing that is when i write wrong username and password so i am able to get user data but when i enter correct user information so i am getting exception that is invalid credentials. please help me

here is my model code

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class email extends Model
{
    protected $table = 'users';
}

&
Here is my Controller Code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\email;

class EmailController extends Controller
{
   public function databyemail(Request $request){   
       $data = email::where('email', $request->email)->first();  
       if($data->password === bcrypt($request->password)) 
       {
           return response()->json($data);
       } 
       else
       {      
           return response()->json(['error' => 'Incorrect credentials!']);  
           } 
           }

} 
How to&Answers:

Can you try Hash::check method for matching password.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use App\email;

class EmailController extends Controller
{
   public function databyemail(Request $request){   
        $data = email::where('email', $request->email)->first(); 


        if( Hash::check( $request->password, $data->password ) ) {
            return response()->json($data);
        }else{
            return response()->json(['error' => 'Incorrect credentials!']);  
        }
    }
}