我现在正在努力处理人际关系,希望能得到一些帮助,如何使这种关系发挥作用。我正在使用Laravel。
让我们假设你有一个员工模型,看起来像这样:
Staff.php
class Staff extends 'Eloquent {
protected $fillable = [];
public function status()
{
return $this->belongsTo('Staff_status');
}
}
员工的数据库表如下:
Table Name: staff
Fields: id, staffer_name, status_id
您还有如下所示的人员状态模型:
Staff_statuses.php
class Staff_status extends Eloquent {
public function staff()
{
return $this->hasMany('Staff');
}
}
您还有一个人员数据库表,如下所示:
Table Name: staff_statuses
Fields: id, status_name
然而,当我尝试加载人员控制器索引方法时,它说类Staff_status未找到。
知道为什么吗?
您使用Staff_statuses.php
作为model
的名称,但您在类名称中使用Staff_status
,因此您也使用控制器中的Staff_status
来调用它。这就是问题所在
更改文件名以匹配class name
。例如,使用如下内容:
// StaffStatus.php
class StaffStatus extends Eloquent{
protected $table = 'staff_statuses';
public function staff()
{
return $this->hasMany('Staff');
}
}
// Staff.php
class Staff extends Eloquent {
protected $table = 'staff';
protected $fillable = [];
public function status()
{
return $this->belongsTo('StaffStatus');
}
}
在controller
中,你可以这样使用:
$staffStatus = StaffStatus::all();
$staff = Staff::all();
名称空间的应用程序;
数据库使用说明' '雄辩'模型;
类照片扩展模型{
public function imageable()
{
return $this->morphTo();
}
}
类Staff扩展Model{
public function photos()
{
return $this->morphMany('App'Photo', 'imageable');
}
}
类Product扩展了Model{
public function photos()
{
return $this->morphMany('App'Photo', 'imageable');
}
}