我有一个数据透视表,其中包含额外的数据。因此,我创建了一个自定义的数据透视模型,如Laravel 5.6文档中所示。
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class PersonaTreeleave extends Pivot
{
public $timestamps = false;
protected $table = 'persona_treeleave';
protected $fillable = [
'FK', 'RoleTitle', 'Merkmale'
];
public function treeleave_id(){
return $this->hasOne('App\Treeleave');
}
public function persona_id(){
return $this->hasOne('App\Persona');
}
}
在控制器文件中,我要将用户附加到已经存在的“ treeleave”上。
App\Treeleave::where('cid',$P['OE'])
->where( 'tree', $Baum->id)->first()
->Persons()->attach($DBUser, [
'FK' => $P['FK'],
'RoleTitle' => $P['RoleTitle'],
'Merkmale' => json_encode($P['Merkmale'])
]
);
我不断收到类似“找不到类'App \ PersonaTreeleave'”的错误。
我不明白为什么会这样。如果在控制器文件中添加“ Use App \ PersonaTreeleave”,则无济于事。
如果我这样做
dump(class_exists('App\Treeleave'));
dump(class_exists('App\PersonaTreeleave'));
它生成以下输出:
true
false
有人暗示吗?
“ treeleave”和“ persona”类
namespace App;
use Illuminate\Database\Eloquent\Model;
class Treeleave extends Model
{
protected $table = 'treeleaves';
public $timestamps = false;
protected $fillable = ['parent','lft','rgt','ebene','oe_titel','tree','meta'];
public function Baum(){
return $this->belongsTo('App\Tree');
}
public function Persons(){
return $this->belongsToMany('App\Persona')
->withPivot('FK', 'RoleTitle', 'Merkmale')
->using('App\PersonaTreeleave')
;
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class Persona extends Model
{
public $timestamps = false;
protected $attributes = [
'titel' => ''
];
protected $fillable = [
'nachname', 'vorname', 'titel', 'projekt', 'email', 'geschlecht', 'cid'
];
public function logins(){
// erwartet Relations-Tabelle "login_project" (alphabetische Reihenfolge der beteiligten Tabellen, Namen im Singular)
return $this->belongsToMany('App\Login');
}
public function OE(){
return $this->belongsToMany('App\Treeleave')
->withPivot('FK', 'RoleTitle', 'Merkmale')
->using('App\PersonaTreeleave')
;
}
public function setTitelAttribute($value)
{
$this->attributes['titel'] = (string)$value;
}
}
答案 0 :(得分:1)
尝试运行:
composer dump-autoload
要使用新的班级信息更新自动加载文件。