Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 Error App\User cannot use Illuminate\Foundation\Auth\User - it is not a trait

I am pretty new to laravel 5.2. Right now I am trying to create a custom login. I created the login html. And on submit, the function shows error

App\User cannot use Illuminate\Foundation\Auth\User - it is not a trait

I couldn't find what is the error. The codes are below

Routes.php

Route::post('/', 'Admin\LoginController@postLogin');

LoginController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Auth;
use Request;


class LoginController extends Controller {

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

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

public function postLogin() {
    $email = Request::input('email');
    $password = Request::input('password');

    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        //echo "success";
        return redirect('admin/dashboard');
    } else {
        return redirect('/');
    }
}

}

User.php

<?php

namespace App;

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

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

use Authenticatable,
    CanResetPassword;

protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['keyword', 'remember_token'];

}

Auth.php

<?php

return [



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

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'users' => [
        'driver' => 'eloquent',
        'model' => 'App\User',
        'table' => 'users',
        'password' => [
            'email' => 'emails.password',
            'expire' => 60,
        ],
    ],
],

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

I am following the tutorial from here

Please help me. I am stuck here from last few hours

like image 833
Arun Avatar asked May 15 '16 09:05

Arun


1 Answers

You should use this namespace:

use Illuminate\Auth\Authenticatable;

It's a trait you should use. Now, you're trying to use a class as a trait:

use Illuminate\Foundation\Auth\User as Authenticatable;

class User {

    use Authenticatable ...
like image 166
Alexey Mezenin Avatar answered Oct 22 '22 22:10

Alexey Mezenin