我有如下表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_categories
。 name
(我的印象是模特的定义会直接帮助拉相关款)
有什么可以解决的?
感谢它的工作,但我对模型做了小调整 – sumit