cola's avatar
Level 1

How can I get password from users table from eloquent object?

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Http\Resources\UserResource;
use Illuminate\Support\Facades\Hash;
use App\Models\User;

class UserController extends Controller
{
    public function login(Request $request){
        
        $email=$request->get('email');
        $password=$request->get('password');
        $user=[
            "email"=>$email,
            "password"=>$password
        ];        
        $model = User::where('email', '=', $email);
        dd($model);
        return response()->json($user);
      }
    
}

How can I get password from users table? After $model = User::where('email', '=', $email); how can I get password from eloquent object?

0 likes
5 replies
Snapey's avatar

you are just returning the data received in the request ?

belisar's avatar

First:

$model = User::where('email', '=', $email);

returns a query builder, not a model.

$model = User::where('email', '=', $email)->first();

will return the model.

Then $model->password will give you the hashed password, which you can check against your request password, but Laravel has built-in ways to handle this. The way you have written the code there, this would be Auth::attempt($user). Although it might be better to rename that $user variable to credentials and $model to $user.

Snapey's avatar

hey @cola

Is your problem resolved? Its nice to reply to people that try to help you,

1 like

Please or to participate in this conversation.