MehulBawadia's avatar

Laravel 5: Multiple User Authentication System

I am trying to make an eCommerce web application using Laravel 5. But it seems that I am stuck at the very first stage of it - Authentication. Well, In my application, there are 2 entities (Administrator and the Normal User).

I have created the administrator table having columns id, name, email and password using the migration.

After some research, I came to know that in the Administrator Model, I have to declare the following code :=>

protected $table = 'administrators';

So I tried that, but that didn't helped me out.

What could be the issue ?

0 likes
10 replies
MattCroft's avatar

Sounds like you have a table called administrator and a plural version referenced in the model?

Can you check to make sure the names of the tables match?

MehulBawadia's avatar

My table name is administrators and my Model name is Administrators

DomHutton's avatar

Model name should be Administrator and table name declared in model should be Administrators.

MehulBawadia's avatar

Will that work ? Asking because I am new to Laravel framework.

DomHutton's avatar

Convention would be 'pluralTableName' and 'singularModelName'

So table: administrators Model: Administrator

Though as long as you have your table name defined correctly in your model, no issue.

MehulBawadia's avatar

Okay.. Here's what I have tried so far..

My AdministratorController:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Auth;

use Illuminate\Http\Request;


class AdministratorController extends Controller {

    public function index() {
        return view('admin.admin_login');
    }

    public function postLogin(Request $request) {
        $this->validate($request, 
            ['email' => 'required', 'password' => 'required'
        ]);
        $credentials = $request->only('email', 'password');

        if (\Auth::attempt($credentials)) {
            $name = \Auth::user()->name;
            return 'Welcome Admin';
        }
    }

}

My Administrator Model:

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class Administrator extends Model implements AuthenticatableContract {

    use Authenticatable;

    protected $table = 'administrators';

    protected $fillable = [
        'name', 'email', 'password'
    ];

}

And here's the table migration:


class CreateAdministratorsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('administrators', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->timestamps();
        });
    }

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

}

MattCroft's avatar

Do you have the controller setup in the routes.php?

frezno's avatar

you should think about it.

You have at least 3 Users:
Adiminstrator, Logged In User, Guest

Now, what about a dealer, or someone who gets special rates?
What if you're selling to other countries? etc etc.

Build in a 'real' user-role system and you will be happy in the future as well.

MehulBawadia's avatar

@frezno I agree to what you are trying to say. But I am at the learning stage of Laravel.

@MattCroft Here's the routes.php code:

Route::get('admin', 'AdministratorController@index');

Route::post('admin/login', 'AdministratorController@postLogin');

When trying to login, I get the following error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'LaraJect.users' doesn't exist (SQL: select * from `users` where `email` = myemail@gmail.com limit 1)
shahinul87's avatar

In your model you just add

protected $table = 'administrators(or whatever the table name is)';

and try to avoid typos.

Please or to participate in this conversation.