2017-10-20 42 views
3

嘗試在整個互聯網上搜索這個錯誤,但都是徒勞的,所以作爲最後的手段,我在StackOverflow上創建了一個問題。LogicException消息'...必須返回關係實例。'

我有兩個簡單的雄辯模型建立:
1.教師(擴展真僞) - 自從我使用MultiAuth的系統。
2. GeneralNotice(延伸雄辯/型號)

app\Teacher.php

public function generalNotices() 
{ 
    $this->hasMany('App\Modules\GeneralNotice'); 
} 

app\Modules\GeneralNotice.php

public function teacher() 
{ 
    $this->belongsTo('App\Teacher'); 
} 

這是我通則遷移表:

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(); 
    }); 

我也有樣品通知播種數據庫,以檢查是否正常工作。

當我試圖訪問任何這些關係時,我遇到了一個錯誤。

$teacher = Teacher::find(1); $teacher->generalNotices;

LogicException與消息 '應用程序\教師:: generalNotices必須返回一個關係實例。'

$notice = GeneralNotice::find(1); $notice->teacher;

LogicException與消息 '應用程序\模塊\ GeneralNotice ::老師必須返回一個關係實例。'

如果我嘗試訪問的成員函數,
$teacher->generalNotices() OR
$notice->teacher()

我得到

請幫忙。

回答

9

您需要return關係,例如:

public function teacher() 
{ 
    return $this->belongsTo('App\Teacher'); 
} 
+4

我爲我的愚蠢道歉。有用! –

+0

@AmruthPillai我只是做了和你一樣的東西,只是遇到這個答案我突然意識到了! – Chud37

相關問題