Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance 在Laravel 4中,从雄辩模型继承的属性为空_Inheritance_Attributes_Null_Laravel 4_Superclass - Fatal编程技术网

Inheritance 在Laravel 4中,从雄辩模型继承的属性为空

Inheritance 在Laravel 4中,从雄辩模型继承的属性为空,inheritance,attributes,null,laravel-4,superclass,Inheritance,Attributes,Null,Laravel 4,Superclass,基本上,我的问题是我的模型没有从它们的超类继承所需的属性。我已经找到了这个问题:,它解决了同样的问题。然而,这个解决方案对我不起作用 我试过了,但是没有设置可填充属性。我的子类没有访问属性的权限 也许我做错了什么 额外信息(我想不是必需的) 我的情况是这样的:用户(表“用户”)可以是顾问(表“顾问”)和/或客户(表“客户”) 所有关于用户的一般信息;名,姓。。。存储在用户表中。特定信息(如客户编号或功能)存储在拨款表中。顾问和客户都有不同的关系,因为他们在应用程序中的角色不同 我设计了我的模

基本上,我的问题是我的模型没有从它们的超类继承所需的属性。我已经找到了这个问题:,它解决了同样的问题。然而,这个解决方案对我不起作用

我试过了,但是没有设置可填充属性。我的子类没有访问属性的权限

也许我做错了什么


额外信息(我想不是必需的)

我的情况是这样的:用户(表“用户”)可以是顾问(表“顾问”)和/或客户(表“客户”)

所有关于用户的一般信息;名,姓。。。存储在用户表中。特定信息(如客户编号或功能)存储在拨款表中。顾问和客户都有不同的关系,因为他们在应用程序中的角色不同


我设计了我的模型,以便Advisor和Customer从super class用户继承:

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $fillable = array('email', 'first_name', 'last_name', 'email', 'gender', 'phone_number', 'profile_picture');
    protected $hidden = array('password');
    protected $guarded = array('id', 'password');

    protected $table = 'users';

    ...

}
我的导师班:

class Advisor extends User {

    protected $table = 'advisors';
    protected $fillable = array('active', 'function', 'description') ;

    //this does not work!
    public function __construct (array $attributes = array()) {
        // the static function getFillableArray() just returns the fillables array      
        $this->fillable = array_merge ($this->fillable, parent::getFillableArray());
        parent::__construct($attributes);
    }
    ...
 }
我还尝试在设置可填充项之前调用构造函数,如下所示:。也不管用

有效的方法是在用户超类中编写访问器,如下所示:

// Attribute getters - Inheritence not working
public function getFirstNameAttribute($value)
{
    $returnValue = null;
    if($value){
        $returnValue = $value;
    }else{
        $returnValue = User::find($this->id)->first_name;
    }
    return $returnValue;
}
但这是丑陋的,没有效率的,而且有明显的原因。 真的没有办法继承这些属性吗?我错过了什么


由于您已经在数据库中设计了一个表继承结构,所以您可以使用此处解释的Laravel eloquent relationship函数,这是解决问题的另一种方法。这将允许您访问超类的属性,例如:

//in your Advisor model
public function profile()
{
    return $this->belongsTo('User');
}

//to call for advisor's first name
Advisor::find($id)->profile->first_name;

天哪,我完全忽略了这一点。尽管我希望能够跟踪类本身的属性。但我认为这是一个可以接受的答案。我将在本周晚些时候发布我的解决方案。我使用$appends属性,这样我就可以用它们填充用户类,但基本上这是您建议的解决方案。谢谢