欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Auth::user() 得不到完整的对象 Calling User Model after being authenticated is producing an error Call to undefined method Illuminate\Auth\GenericUser FatalErrorException : Call to undefined method Illuminate\Auth\GenericUser::save() 有大用

auth.php Model user eloquent providers driver users Auth
Auth::user() 得不到完整的对象 模型  配置提供者驱动
正确答案 

Make sure that app/config/auth.php  (laravel 5.2是 config/auth.php )has 'driver' => 'eloquent'. So it will use your Eloquent User model.
好像改成了 
'providers' => [
        'users' => [
           'driver' => 'eloquent',
           'model' => App\User::class,
           'table' => 'users'
        ],

   //      'users' => [
    //         'driver' => 'database',
    //         'table' => 'users',
    //     ],
    ],

3
 

I have been trying to execute a Method in the User Model after being logged in. It is my understand that you do the following: Auth::user()->foo();

For some reason this is not working.

Code in User.php

<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;
    protected $table = 'users';
    protected $hidden = array('password', 'remember_token');

    public function foo(){
        return "foo";
    }
}

Code in routes.php

<?php
Route::get('/', function(){
    return View::make('hello');
});

Route::get('users', function(){
    if (Auth::attempt(array('email' => 'tom@blah.com', 'password' => 'Tom')))echo 'Auth-true';
    echo Auth::id();
    if(Auth::check())echo 'checked';
    Auth::user()->foo();
});

echo Auth::id() and the Auth:check() both work, Auth:user()->foo(); fails

Error Message

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Call to undefined method Illuminate\Auth\GenericUser::foo()

I have been trying to execute a Method in the User Model after being logged in. It is my understand that you do the following: Auth::user()->foo();

For some reason this is not working.

Code in User.php

<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;
    protected $table = 'users';
    protected $hidden = array('password', 'remember_token');

    public function foo(){
        return "foo";
    }
}

Code in routes.php

<?php
Route::get('/', function(){
    return View::make('hello');
});

Route::get('users', function(){
    if (Auth::attempt(array('email' => 'tom@blah.com', 'password' => 'Tom')))echo 'Auth-true';
    echo Auth::id();
    if(Auth::check())echo 'checked';
    Auth::user()->foo();
});

echo Auth::id() and the Auth:check() both work, Auth:user()->foo(); fails

Error Message

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Call to undefined method Illuminate\Auth\GenericUser::foo()


正确答案 


3down voteaccepted

Make sure that app/config/auth.php has 'driver' => 'eloquent'. So it will use your Eloquent User model.
'providers' => [

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

   //      'users' => [
    //         'driver' => 'database',
    //         'table' => 'users',
    //     ],
    ],

普通分类: