Php Laravel 4-选择相关型号

Php Laravel 4-选择相关型号,php,orm,laravel,laravel-4,eloquent,Php,Orm,Laravel,Laravel 4,Eloquent,我有下表 Schema::create('jokes_categories', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('is_active'); $table->timestamps(); }); Schema::create('jokes', function(Blueprint $tab

我有下表

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\u id
中的是一个外键,它与
笑话\u 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();
在控制器中,我有以下内容:

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();
但是,它并没有完成分类(我的印象是模型定义将直接帮助拉动相关模型)


解决方案是什么?

您的查询就在笑话表上

您可以加载类别,即

$jokes = Joke::with('JokesCategory')->get();

请参阅文档:

该约定实际上是camel case而不是pascal case,否则Laravel似乎不会自动加载关系。我犯了同样的错误,不明白为什么我的关系没有自动加载

public function jokesCategory()