2014-04-17 33 views
1

我有如下表Laravel 4 - 选择相关的模型

Schema::create('jokes_categories', function(Blueprint $table) { 
    $table->increments('id'); 
    $table->string('name'); 
    $table->string('is_active'); 
    $table->timestamps(); 
}); 

Schema::create('jokes', function(Blueprint $table) { 
    $table->increments('id'); 
    $table->string('content', 200)->unique();; 
    $table->enum('is_active', array('Y', 'N')); 
    $table->integer('category_id')->unsigned(); 
    $table->foreign('category_id')->references('id')->on('jokes_categories'); 
    $table->timestamps(); 
}); 

在笑话表category_id是一个外键,它与jokes_categories

一个一对多的关系,在模型我有以下内容:

class Joke extends \Eloquent { 

    public static $rules = array(); 

    // Don't forget to fill this array 
    protected $fillable = array(); 

    public function JokesCategory(){ 
     return $this->belongsTo('JokesCategory'); 
    } 
} 

在控制器中我有以下内容:

$jokes = Joke::all(); 

但它并没有通过joke_categoriesname(我的印象是模特的定义会直接帮助拉相关款)

有什么可以解决的?

回答

0

的约定实际上是骆驼,而不是帕斯卡情况下,否则Laravel似乎并没有自动加载的关系。我犯了同样的错误,不知道为什么我的关系不能自动加载。

public function jokesCategory()