1

我有一个扩展类用户

<?php

namespace App;

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

我有木屋课

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()

这是扩展用户类的正确方法吗?

我已经搜索了扩展 User 类的方法。我发现了这个问题:Laravel 中的模型继承并没有帮助我。

我正在使用 Laravel 5.7

4

1 回答 1

1

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

  1. 运行以下命令

    php artisan vendor:publish --provider="Cartalyst\Sentinel\Laravel\SentinelServiceProvider"
    
  2. 在 'config\cartalyst.sentinel.php' 打开已发布的配置文件

  3. 将其从以下内容修改 'users' => [ 'model' => 'Cartalyst\Sentinel\Users\EloquentUser', ], 为: 'users' => [ 'model' => 'App\User', ],

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

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

Sentinel::getUserRepository()->setModel('App\User');
于 2018-12-24T08:01:06.393 回答