GAL
GAL

Reputation: 39

custom table in user model laravel

I'm using oauth2 and my table users is "coUsers" . I added this in my User Model

App\User

protected $table = 'coUsers';
public function getAuthPassword()
    {
        return $this->pass;
    }

AuthController

public function login(Request $request)
{
    $request->validate([
        'usuario' => 'required|string|email',
        'clave' => 'required|string',
        //'remember_me' => 'boolean'
    ]);
    $credentials = [
        'usuario' => $request->get('usuario'),
        'password' => $request->get('clave'),
    ];

    if(!Auth::attempt($credentials)){
        return response()->json([
            'message' => 'Unauthorized'
        ], 401);
    }

    $user = $request->user();
    $tokenResult = $user->createToken('Personal Access Token');
    $token = $tokenResult->token;
    if ($request->remember_me)
        $token->expires_at = Carbon::now()->addWeeks(1);

    $token->save();

    return response()->json([
        'access_token' => $tokenResult->accessToken,
        'token_type' => 'Bearer',
        'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString()
    ]);
}

public function firstLogin(Request $request)
{
    $request->validate([
        'usuario' => 'required|string|email|unique:users',
        'clave' => 'required|string',
        'nuevaClave' => 'required|string'
    ]);

    $user = User::where('usuario', $request['usuario'])
                ->where('clave', $request['clave'])
                ->first();

    $user->clave = bcrypt($request['nuevaClave']);
    $user->first_login = false;
    $user->save();
    return response()->json([
        $user->toArray()
    ]);
}

Auth login works OK, but I want to use User::where in firstLogin.... I get this error:

Illuminate\Database\QueryException: SQLSTATE[42703]: Undefined column: 7 ERROR: column "usuario" does not exist LINE 1: select count() as aggregate from "users" where "usuario" = ... ^ (SQL: select count() as aggregate from "users" where "usuario" = [email protected]) in file \vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 669

Look in the users table instead of using the table that I indicated in the model.

Upvotes: 0

Views: 711

Answers (1)

Foued MOUSSI
Foued MOUSSI

Reputation: 4813

You may change 'usuario' => 'required|string|email|unique:users', to 'usuario' => 'required|string|email|unique:coUsers', in your firstLogin method

You may also change this 'unique:users' in validator method inside your App\Http\Controllers\Auth\RegisterController

'email' => ['required', 'string', 'email', 'max:255', 'unique:users']

to

protected function validator(array $data)
{
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:coUsers'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
}

Upvotes: 1

Related Questions