TheBAST
TheBAST

Reputation: 2736

How to get the value of a variable from the collection

   public function returnsTrueIfEmailIsVerified(Request $request)
    {
        // Gets the email
        $email = request("email"); //[email protected] for example
        // Determine if zero or one ;
        $user  = User::where('email','=',$email)->get(); // 0 or 1  ;

        $userCount  = User::where('email','=',$email)->count(); // 0 or 1  ;

        $confirmedValue = $user->get('confirmed');


        $response ;

        if ( $user === 1 && $confirmedValue === true ) {
            $response = 'OK';
        }
        elseif ($user === 1 && $confirmedValue === false) {
            $response = 'Not Confirmed yet';
        }
        else { 
            $response = 'Not Registered yet';
        }
        return response()->json(200,$response);

    }

with that code I would return a response that if a user isn't registered or is registered and that if he's registered but that he's not confirmed yet..

I want to return something out from that I'm not just familiar with laravel's way

Upvotes: 0

Views: 88

Answers (1)

Kyaw Kyaw Soe
Kyaw Kyaw Soe

Reputation: 3360

There are so many error in your code, I've fixed it and this code will work. I think you need to learn more Laravel and PHP.

public function returnsTrueIfEmailIsVerified(Request $request)
{
    $email = request("email");
    $user = User::where('email', '=', $email);

    $response = [
        'message' => ''
    ];

    if ($user->count() === 1) {
        $confirmedValue = $user->first()->confirmed;
        if ($confirmedValue) {
            $response['message'] = 'OK';
        } else {
            $response['message'] = 'Not Confirmed yet';
        }
    } else {
        $response['message'] = 'Not Registered yet';
    }

    return response()->json($response, 200);
}
  • you can't response string as a json, json is key value pair.
  • User::where('email', '=', $email) return Query Builder not 0 or 1, use count() method;
  • you can't retrieve value from multiple items you have to specific item like this $user->get()[0]['confirmed] or use Laravel special method $user->first()->confirmed

Upvotes: 1

Related Questions