0

I am building a API using laravel, and i am trying to create a function to find a user in data base using a email as a parameter

public function find_user(Request $request){
    try{
        $user = UserAccess::table('user_accesses')->where('email', $request->email)->first();
        return ['api_request_return'=>'ok', 'return' => $user];

    }catch(\Exception $error){
        return ['api_request_return' => 'error', 'error_details' => $error];
    }
}

The API route is ok, but its always returning error:

{
     "api_request_return": "error",
     "error_details": {}
}
3
  • 1
    Please try to use DB::table('user_accesses')->where('email', $request->email)->first(); Commented May 9, 2020 at 19:53
  • Because you are using DB and Eloquent together. Kindly use one. Commented May 9, 2020 at 19:54
  • Or, go with the model way i.e. UserAccess::where('email', $request->email)->first(); Commented May 9, 2020 at 20:31

1 Answer 1

1

Use the corrected one

public function find_user(Request $request){
    try{
        $user = DB::table('user_accesses')->where('email', $request->email)->first();
        return ['api_request_return'=>'ok', 'return' => $user];

    }catch(\Exception $error){
        return ['api_request_return' => 'error', 'error_details' => $error];
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

When i try to use DB::table, its warning "Undefined type 'DB'.intelephense(1009)"
from IDE ibb.co/Rzv0TW0, but when i make request, is returning null value: { "api_request_return": "ok", "return": null }
Check whether user_accesses table have the given email record in database. Its return null when not found
This is the table: ibb.co/q0H2SNY and GET request: ibb.co/P5qqMZ4
Please dd the value $request->email ->>>>>>>>>>>>>> die(print_r($request->email)) and check whether variable has value in it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.