【问题标题】:eloquent relations with extend user model sentinel laravel package与扩展用户模型哨兵 laravel 包的雄辩关系
【发布时间】:2019-03-25 15:04:52
【问题描述】:

我有一个扩展类用户

<?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类是正确的方法吗?

我已经搜索了扩展 User 类的方法。我发现了这个问题:Model Inheritance in Laravel 不过对我没有帮助。

我正在使用 Laravel 5.7

【问题讨论】:

    标签: php laravel-5 eloquent cartalyst-sentinel


    【解决方案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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-10
      • 2019-02-12
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2013-06-04
      • 2016-11-26
      • 2018-02-19
      相关资源
      最近更新 更多