Reputation: 1693
Tried searching the entire internet for this error, but all in vain, so as a last resort, I'm creating a question here on StackOverflow.
I have two simple Eloquent Models set up:
1. Teacher (extends Authenticable) - since I'm using MultiAuth for the system.
2. GeneralNotice (extends Eloquent/Model)
app\Teacher.php
public function generalNotices()
{
$this->hasMany('App\Modules\GeneralNotice');
}
app\Modules\GeneralNotice.php
public function teacher()
{
$this->belongsTo('App\Teacher');
}
This is my migration table for General Notices:
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();
});
I have also seeded the database with a sample notice, to check if it works.
When I try to access any of these relations, I run into an error.
$teacher = Teacher::find(1);
$teacher->generalNotices;
LogicException with message 'App\Teacher::generalNotices must return a relationship instance.'
$notice = GeneralNotice::find(1);
$notice->teacher;
LogicException with message 'App\Modules\GeneralNotice::teacher must return a relationship instance.'
If I try to access the member function,
$teacher->generalNotices()
OR
$notice->teacher()
I get null.
Please help.
Upvotes: 16
Views: 22481
Reputation: 163788
You need to return
relationship, for example:
public function teacher()
{
return $this->belongsTo('App\Teacher');
}
Upvotes: 43