2017-02-05 6 views
0

laravel 4からlaravel 5に移行しました。ユーザーモデルをlaravel 5 User Modelと互換性があるように変更しました。 Laravel 4におけるユーザモデルはそのようなものです:Laravel 5の "User Model"機能が動作しません

use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableInterface; 

class User extends Eloquent implements UserInterface, RemindableInterface { 
    // some model relation functions.. 
    ... 
    public function getLocationId() 
    { 
     return $this->location_id; 
    } 
} 

私はそれを修正して、新しいユーザー・モデルは今そのようなものです:

namespace App\Models; 

use Illuminate\Auth\Authenticatable; 
use Illuminate\Auth\Passwords\CanResetPassword; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; 

use Illuminate\Database\Eloquent\Model as Eloquent; 

class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract { 

    use Authenticatable, CanResetPassword; 
    // some model relation functions (some changes for namespace) 
    ... 
    public function getLocationId() 
    { 
     return $this->location_id; 
    } 
} 

しかし、getLocationId方法について問題があります。次の行はエラーになります。

$auth_location_id = Auth::user()->getLocationId(); 

これはエラーです:

BadMethodCallException in Builder.php line 2508: 
Call to undefined method Illuminate\Database\Query\Builder::getLocationId() 

にはどうすれlaravel 5に方法が互換性を持たせることができますか?おかげで..

更新:

$auth_location_id = Auth::user()->getLocationId;作品だが、空を返します!

以下の行は正しく動作します。

私の理解することで
$auth_user_id = Auth::user()->getAuthIdentifier(); 
+0

laravelのバージョンは何ですか、あなたは更新しようとしていますか? 5.4?もしそうなら、このリンク[https://github.com/laravel/laravel/blob/master/app/User.php] (https://github.com/laravel/laravel/blob/master/app/User.php)に従ってください。 ) – zgabievi

+0

@zgabieviはい、5.4です。 – horse

答えて

0

、およびLaravelと短い経験、私はすべてがちょうど使用することです:

use Illuminate\Foundation\Auth\User as Authenticatable; 

次にとしての私のモデルを持っている:

use Illuminate\Foundation\Auth\User as Authenticatable; 
class User extends Authenticatable 
{ 
    public function getLocationId() 
    { 
     return $this->location_id; 
    } 
} 

だから私は安全であり少なくとも、それはすべての契約が....\Userが使用されるファサードで延長されるということが私のために働くからです。

PS: this is just an illustration of how I currently use it in Laravel 5.2.*.

これが役立つか解にあなたを導くことができると思います:)

関連する問題