1

I'm trying to authenticate a user from the table called "cients_table"

Migration

 public function up()
{
    Schema::create('clients_table', function (Blueprint $table) {
        $table->increments('id');
        $table->string('fullname');
        $table->string('email')->unique();
        $table->string('username')->unique();
        $table->string('password');
        $table->text('address');
        $table->string('phone_number');
        $table->string('location');
        $table->string('gender');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('clients_table');
}

And i read that to specify the table your model should work with you should and it the $table to it. So in my Client model this is what's there

namespace App;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class ClientModel extends Model implements Authenticatable
{
    use \Illuminate\Auth\Authenticatable;
    public $table = 'Clients_table';
}

In my controller. This is what i'm trying to authenticate the user with

public function client_login(Request $request){
    if (Auth::attempt(['email' => $request['email'], 'username' => $request['username']])){
        return redirect()->route('client_dashboard');
   }
    return redirect()->back();
}

but it's giving me this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'where 
clause' (SQL: select * from `users` where `email` = chinonsoeke@gmail.com 
and `username` = king_eke limit 1)

apparently it's not using the right table to fetch the info from.. Please what can I do ??

3 Answers3

1

first change:

public $table = 'Clients_table';

to

public $table = 'clients_table';

after that go to config/auth.php and change:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model'  => App\User::class,
            'table'  => 'users',
        ],
    ],

to

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model'  => App\ClientModel::class,
            'table'  => 'clients_table',
        ],
    ],

if you want a proper username authentification, just look at this: https://stackoverflow.com/a/45572527/4369087

yoeunes
  • 2,927
  • 2
  • 15
  • 26
  • Thanks that works.. but i have more than one table.. so what would i add to the 'providers' to make it also query the second table when i want to authorize someone from that table – Chinonso Eke Aug 11 '17 at 17:51
0

Well you have changed the default Auth config by renaming the Model! so obviously it won't work, you can notice that from the query, it's still searching in the users table.

to fix that (a better way is to do it laravel way, but you can customize of course like follows):

1- Go to config/auth.php and change your providers model value to your users model name:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\ClientModel::class,
        ],
]

2- second option is to duplicate the same settings with your new configuration, just follow what's there inside config/auth.php and duplicate everything with your own settings.

Sletheren
  • 2,435
  • 11
  • 25
  • Thanks that works.. but i have more than one table.. so what would i add to the 'providers' to make it also query the second table when i want to authorize someone from that table – Chinonso Eke Aug 11 '17 at 17:51
0

I think you can try this:

Change

public $table = 'Clients_table';

to

public $table = 'clients_table';

AND change below code in config/auth.php:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model'  => App\ClientModel::class,
            'table'  => 'clients_table',
        ],
    ],

After clear cache and try:

php artisan config:cache
php artisan cache:clear
php artisan config:clear
halfer
  • 19,824
  • 17
  • 99
  • 186
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • Thanks that works.. but i have more than one table.. so what would i add to the 'providers' to make it also query the second table when i want to authorize someone from that table – Chinonso Eke Aug 11 '17 at 17:25