0

i create a new table with name of students

public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            //   $table->increments('id');
            $table->engine = 'InnoDB';

            $table->string('roll_no');
            $table->string('name');
            $table->string('father_name');
            $table->string('address');
            $table->string('cnic');
            $table->string('phone_no');
            $table->string('father_phone_no');
            $table->string('email')->unique();
            $table->string('password');
//            $table->string('dept_id');
            $table->timestamps();

            $table->primary('roll_no');
//            $table->foreign('dept_id')->references('dept_id')->on('departments')->onDelete('cascade');
        });
    }

then i create a student model that is empty

class student extends Authenticatable
{
    //
}

then i make changes in my auth.php file

<?php

return [


    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'student' => [
            'driver' => 'session',
            'provider' => 'students',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

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

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'students' => [
            'provider' => 'students',
            'email' => 'student.auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

then i add a middleware RedirectifNotStudent

public function handle($request, Closure $next, $guard = 'student')
    {
        if (!Auth::guard($guard)->check()) {
            return redirect('/student/login');
        }

        return $next($request);
    }

then i add this line to kernel.php file

'student' => \App\Http\Middleware\RedirectIfNotstudent::class,

after that i add a login, logout, registration function in that directory StudentAuth/AuthController

    protected $redirectTo = '/student';
        protected $guard = 'student';
public function showLoginForm()
    {
        if (Auth::guard('student')->check())
        {
            return redirect('/student');
        }

        return view('student.auth.login');
    }

    public function showRegistrationForm()
    {
        return view('student.auth.register');
    }

    public function resetPassword()
    {
        return view('student.auth.passwords.email');
    }

    public function logout(){
        Auth::guard('student')->logout();
        return redirect('/student/login');
    }

After that i add Employee controller in Student/Employee like this

    class Employee extends Controller
{
    public function __construct(){
        $this->middleware('student');
    }

    public function index(){
        return view('Student.home');
    }
}

this my route file

    Route::get('/student/login','StudentAuth\AuthController@showLoginForm');
Route::post('/student/login','StudentAuth\AuthController@login');
Route::get('/student/password/reset','StudentAuth\PasswordController@resetPassword');

Route::group(['middleware' => ['student']], function () {
    //Login Routes...
    Route::get('/student/logout','StudentAuth\AuthController@logout');

    // Registration Routes...
    Route::get('student/register', 'StudentAuth\AuthController@showRegistrationForm');
    Route::post('student/register', 'StudentAuth\AuthController@register');

    Route::get('/student', 'Student\Employee@index');
});



Route::group(['middleware' => 'web'], function () {
//    Route::auth();
    Route::get('/home', 'HomeController@index');

builtin authentication is working fine but i want another for student and this error is occur

Authentication user provider [] is not defined.  
1
  • You sure this is the error?? Commented May 21, 2016 at 9:12

1 Answer 1

1

Laravel model assume default primary key as Id and Default Table name as Model class name. And in your Table, You have set 'roll_no' as Primary key and Table name as 'students'.

So you have to tell laravel model for table and primary key value.

Add this line in student model.

protected $table = 'students';
protected $primaryKey = 'roll_no';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.