[英]Laravel 4 Eloquent one-to-many relations error
Using for the first time the Eloquent and i dont understand whats the problem here.. 第一次使用口才,我不明白这里出了什么问题。
class TourItem extends Eloquent {
public function tour()
{
return $this->belongsTo('Tour');
}
}
class Tour extends Eloquent {
public function tourItem()
{
return $this->hasMany('TourItem');
}
}
Here im trying to get the tour itens but only return NULL. 在这里,我试图获取行程,但只返回NULL。
$tour = Tour::where('slug', '=', $postSlug)->where('wrh_id', '=', 1)->first();
if (!is_null($tour)) {
var_dump($tour->tourItem);
foreach ($tour->tourItem as $item) {
var_dump($item->texto);
}
}
DB Schema: 数据库架构:
Schema::create('tours', function($t) {
$t->increments('id');
$t->string('titulo');
$t->string('slug');
$t->boolean('main');
$t->integer('wrh_id');
$t->timestamps();
});
Schema::create('tour_items', function($t) {
$t->increments('id');
$t->string('titulo');
$t->text('texto');
$t->text('saiba_mais');
$t->integer('tour_id');
$t->timestamps();
});
Where is the error?? 错误在哪里?
Thank you Roberto 谢谢罗伯托
EDIT 编辑
I test your migrations and model, and all works without touching anything. 我测试了您的迁移和模型,所有工作都实现了,而没有任何涉及。 I create a repo at github replying your question. 我在github上创建了一个仓库来回答你的问题。
I'm not sure why didn't work for Roberto, but maybe you are doing some mistake in setting your database or migration. 我不确定为什么对Roberto无效,但是也许您在设置数据库或迁移时犯了一些错误。
I hope that the code from repository help you, and make sure to follow the instruction of the Readme. 我希望存储库中的代码对您有所帮助,并确保遵循自述文件的说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.