Laravel 4关系不工作


Laravel 4 relation not working

我有以下迁移代码(简化):

广告表
class CreateAdsTable extends Migration {
public function up()
{
    Schema::create('ads', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('authors_id')->unsigned()->index();
        $table->foreign('authors_id')->references('id')->on('authors');
        $table->timestamps();
    });
}
}

作者表

class CreateAuthorsTable extends Migration {
public function up()
{
    Schema::create('authors', function(Blueprint $table) {
        $table->increments('id');
        $table->string('name', 200);
        $table->timestamps();
    });
}
}

和我的模型是:

广告模式

class Ad extends 'Eloquent {
protected $table = 'ads';
// Ad __hasOne__ Author
public function author() {
    $this->belongsTo('Author');
}
}

作者模型

class Author extends 'Eloquent {
// The database table used by the model
protected $table = 'authors';
// Author __hasMany__ Ads
public function ads() {
    $this->hasMany('Ad');
}
}

但是当我尝试使用Ad::find(1)->author获得作者时,我收到Relationship method must return an object of type Illuminate'Database'Eloquent'Relations'Relation

有人能帮我找出错误吗?

你必须返回它:

return $this->belongsTo('Author');