簡體   English   中英

與擴展用戶模型哨兵 laravel 包的雄辯關系

[英]eloquent relations with extend user model sentinel laravel package

我有一個擴展的類 User

<?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 類的方法。 我發現了這個問題:盡管Laravel 中的模型繼承對我沒有幫助。

我正在使用 Laravel 5.7

你得到的異常表明 Sentinel 仍然指的是默認的 Sentinel 的EloquentUser模型。 請確保您指出了與已發布的哨兵配置您的擴展用戶模型。

  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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM