[英]How to get last 9 records but exclude the record with a certain ID using Eloquent?
[英]Get records with last child record
我有一個包含許多Posts
的Sites
模型。
我想知道什么是最有效的雄辯查詢,可返回所有站點記錄以及1(最新)Post記錄。
我嘗試在我的網站模式中使用它,但是它返回的所有帖子都不是我想要的。
public function lastPost()
{
return $this->hasMany('App\Post')->last();
}
我遇到的另一個問題是嘗試在我的視圖中打印單個列。 {{ $site->lastPack->name }}
返回
$ name未定義的屬性
...盡管這是我的Post模型中的有效列。
將您的lastPost
關系更改為hasOne
如下所示:
public function lastPost()
{
return $this->hasOne('App\Post')->latest('id');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.