See this article: https://medium.com/@nasrulhazim/laravel-using-different-table-and-guard-for-login-bc426d067901
However just a suggestion, I'd stay with users table.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
What I want to do is change where laravel looks for the email and password for registration. Instead of having it in the users table, I have it in a logins table. But even when I change the code in the config/auth.php file it still gives me an error as below:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select count(*) as aggregate from users where email = [email protected])
logins migration file:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;
class CreateLoginsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('logins', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('email'); $table->string('password'); $table->timestamps(); }); }
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('logins');
}
}
users migration file:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->bigIncrements('id'); $table->string('name', 45); $table->mediumText('address'); $table->string('irdNum'); $table->integer('phone'); $table->bigInteger('loginID')->unsigned()->index()->nullable(); $table->timestamps(); });
Schema::table('users', function($table) {
$table->foreign('loginID')->references('id')->on('logins')
->onUpdate('cascade')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Login model
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;
class Login extends Model { use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email',
'password'
];
/**
* The attributes that should be hidden for arrays.
*
User model
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable;
class User extends Authenticatable { use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'address',
'irdNum',
'phone',
'logInID'
];
} RegisteredUserController.php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller; use App\Models\Login; use App\Providers\RouteServiceProvider; use Illuminate\Auth\Events\Registered; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rules;
class RegisteredUserController extends Controller { /** * Display the registration view. * * @return \Illuminate\View\View */ public function create() { return view('auth.register'); }
/**
* Handle an incoming registration request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'address' => 'required|string|max:500',
'irdNum' => 'required|string|max:12',
'phone' => 'required|string|max:16',
'email' => 'required|string|email|max:255|unique:users',
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$login = Login::create([
'email' => $request->email,
'password' => Hash::make($request->password)
]);
$login->save();
$user = User::create([
'name' => $request->name,
'address' => $request->address,
'irdNum' => $request->irdNum,
'phone' => $request->phone,
'loginID' => $login->id
]);
event(new Registered($user));
Auth::login($user);
return redirect(RouteServiceProvider::HOME);
}
}
I went to the below file and hanged all occurrences of users to logins
config/auth.php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'logins',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'logins',
],
'api' => [
'driver' => 'token',
'provider' => 'logins',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'logins' => [
'driver' => 'eloquent',
'model' => App\Models\Login::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'logins' => [
'provider' => 'logins',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];
As seen in the error, it seems to still be looking at the users table for email. I am not why that is happening as I have set everything that was users to logins
Thank you for any future responses, its much appreciated!
Please or to participate in this conversation.