eloquent 与扩展用户模型标记的关系 laravel 包

eloquent relations with extend user model sentinel laravel package

我有一个 class 扩展

的用户
<?php

namespace App;

class User extends \Cartalyst\Sentinel\Users\EloquentUser
{
    public function chalets(){
        return $this->hasMany('App\Chalet');
    }
}

我有木屋 Class

class Chalet extends Model
{
    protected $fillable = [
        'name', 'description',
    ];
public function user(){
        return $this->belongsTo('App\User');
    }
}

而且我有方法可以由用户添加小木屋:

public function postCreateChalet(Request $request){
        $chalet = new Chalet([
            'name' => $request->input('name'),
            'description' => $request->input('description')
        ]);
        Sentinel::getUserRepository()->setModel('App\User');
        $user = Sentinel::getUser();
        $user->chalets()->save($chalet);
        return ('chalet has created');
    }

它给我一个错误:

BadMethodCallException
Call to undefined method Cartalyst\Sentinel\Users\EloquentUser::chalets()

扩展用户 class 的正确方法吗?

我已经搜索了扩展用户 class 的方法。我发现了这个问题:Model Inheritance in Laravel 虽然对我没有帮助。

我正在使用 Laravel 5.7

您得到的异常表明 Sentinel 仍然指的是默认库存 Sentinel 的 EloquentUser 模型。确保使用已发布的 Sentinel 配置指向 您的 扩展用户模型。

  1. 运行 下面的命令

    php artisan vendor:publish --provider="Cartalyst\Sentinel\Laravel\SentinelServiceProvider"
    
  2. 在 'config\cartalyst.sentinel.php'

  3. 打开已发布的配置文件
  4. 根据以下内容修改: 'users' => [ 'model' => 'Cartalyst\Sentinel\Users\EloquentUser', ], 到: 'users' => [ 'model' => 'App\User', ],

更多信息,请参考https://github.com/cartalyst/sentinel/wiki/Extending-Sentinel

通过配置配置后,您将不需要以下行:

Sentinel::getUserRepository()->setModel('App\User');