2014-05-24 111 views
0

我有后续迁移代码(简化):Laravel 4关系不工作

广告表

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

authors表

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

有人可以帮我找到错误吗?

回答

0

您要退回去:

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

多么愚蠢! :) 太棒了!谢谢! –