Amr*_*lai 9 php laravel eloquent laravel-5
试图在整个互联网上搜索这个错误,但都是徒劳的,所以作为最后的手段,我在StackOverflow上创建了一个问题.
我有两个简单的Eloquent模型设置:
1.教师(扩展可验证) - 因为我正在使用MultiAuth系统.
2. GeneralNotice(扩展Eloquent/Model)
app\Teacher.php
public function generalNotices()
{
$this->hasMany('App\Modules\GeneralNotice');
}
Run Code Online (Sandbox Code Playgroud)
app\Modules\GeneralNotice.php
public function teacher()
{
$this->belongsTo('App\Teacher');
}
Run Code Online (Sandbox Code Playgroud)
这是我的一般通知的迁移表:
database/migrations/***_create_general_notices_table.php
Schema::create('general_notices', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longtext('description')->nullable();
$table->string('attachment')->nullable();
$table->integer('teacher_id')->unsigned();
$table->foreign('teacher_id')->references('id')->on('teachers');
$table->date('dated_on')->default(now());
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
我还在数据库中播放了一个示例通知,以检查它是否有效.
当我尝试访问任何这些关系时,我遇到了一个错误.
$teacher = Teacher::find(1);
$teacher->generalNotices;
带有消息'App\Teacher :: generalNotices的LogicException必须返回一个关系实例.'
$notice = GeneralNotice::find(1);
$notice->teacher;
带有消息'App\Modules\GeneralNotice :: teacher的LogicException必须返回一个关系实例.'
如果我尝试访问成员函数,
$teacher->generalNotices()或者
$notice->teacher()
我得到空.
请帮忙.
Ale*_*nin 28
你需要return关系,例如:
public function teacher()
{
return $this->belongsTo('App\Teacher');
}
Run Code Online (Sandbox Code Playgroud)