【发布时间】:2014-07-07 09:12:58
【问题描述】:
我想要获得作者在去年创作的章节。
因此,我有一个作者、书籍和章节模型。
我通过 hasManyThrough-Relationship 进入章节,它在我的 Author-Model 中看起来像这样:
public function chapters() {
return $this->hasManyThrough('Chapter', 'Book');
}
一切正常,因为现在我可以做这样的事情:$authors->chapters
但我想从一个作者那里得到每一章,这是去年创作的:
$chapter[$i] = $author->chapters()->where('created_at', '>', Carbon::now()->subdays(365));
问题是,它不是从章节中获得“created_at”,而是从作者那里获得“created_at”。
如果我尝试这样的事情:
$chapters = $author->chapters;
然后试试:
$chapter = $chapters->where('created_at', '>', Carbon::now()->subdays(365));
我收到消息:“不能在非对象上使用 Where”或类似的东西。
对于如何访问去年的章节,我们将不胜感激。提前致谢。
当我在这篇文章中读到时,我有一种感觉,那就是不可能,而不是不善言辞。我可能在这里错了: Laravel / Eloquent : hasManyThrough WHERE
编辑: 这是我所有的模型、控制器和迁移:
http://help.laravel.io/193c1da926554da8448cad1cc3289535acc53574
【问题讨论】:
-
对不起,我不得不删除我的答案。
withTimestamps()只能在hasMany()或belongsToMany()关系上调用。