访问关系的关系
Accessing relationship of a relationship
我正在为我的应用程序添加公告功能。
我有一个 announcements
table 存储公告和一个 announcement_active
table 存储 undismissed 公告每个用户。
创建公告时,在announcements
table中生成一条记录来存储此消息,并在announcement_active
table中为每个用户输入一条记录对于那个公告。
我的用户模型与 活动公告 保持关系:
public function activeAnnouncements()
{
return $this->hasMany('App\ActiveAnnouncements');
}
然后我将在我看来访问此内容以检查是否有任何 未取消的 公告,如下所示:
@if (Auth::user()->activeAnnouncements()->count() > 0)
//code
@endif
以上工作正常,当我试图对我遇到问题的公告进行 for each
循环时:
@if (Auth::user()->activeAnnouncements()->count() > 0)
@foreach(Auth::user()->activeAnnouncements()->announcement as $announcement)
//code to display announcement
@endforeach
@endif
当我链接 ->announcement
时,我 运行 遇到了麻烦。我这样定义关系:
public function announcement()
{
return $this->belongsTo('\App\Announcements');
}
但是我收到错误:
Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$announcement
使用 Tinker 和 运行ning \App\User::find(1)->activeAnnouncements()->first()->announcement
时会显示正确的公告。
尝试将这些加载到我的 foreach 循环中时我哪里出错了?
您实际上从未在 foreach 中检索数据。相反,Laravel 认为您在说 "build this query, don't execute it, then look for a property called 'announcement' on it"。如果您只是将其更改为 get()
调用,它应该可以工作:
@foreach(Auth::user()->activeAnnouncements()->get() as $announcement)
或者更好的是,使用自动 属性 版本:
@foreach(Auth::user()->activeAnnouncements as $announcement)
顺便说一下,您可能想看看 Blade 的 @each
功能:https://laravel.com/docs/5.3/blade#rendering-views-for-collections
在您的 class 用户中,定义一个 announcements
关系,如下所示:
public function announcements() {
return $this->hasManyThrough(Announcement::class,ActiveAnnouncement::class);
}
然后您可以使用 announcement
访问用户的公告:
@if (Auth::user()->announcements()->count() > 0)
@foreach(Auth::user()->announcements as $announcement)
//code to display announcement
@endforeach
@endif
备注:
也许您可以简单地在公告中添加一列 is_active
并删除 ActiveAnnouncement 模型。这样,在 class User
中定义 announcements
关系:
在您的 class 用户中,定义一个 announcements
关系,如下所示:
public function announcements() {
return $this->hasMany(Announcement::class);
}
并通过以下方式访问活动公告:
$activeAnnouncements = $user->announcements()->whereIsActive(true)->get()
希望这会有所帮助
我正在为我的应用程序添加公告功能。
我有一个 announcements
table 存储公告和一个 announcement_active
table 存储 undismissed 公告每个用户。
创建公告时,在announcements
table中生成一条记录来存储此消息,并在announcement_active
table中为每个用户输入一条记录对于那个公告。
我的用户模型与 活动公告 保持关系:
public function activeAnnouncements()
{
return $this->hasMany('App\ActiveAnnouncements');
}
然后我将在我看来访问此内容以检查是否有任何 未取消的 公告,如下所示:
@if (Auth::user()->activeAnnouncements()->count() > 0)
//code
@endif
以上工作正常,当我试图对我遇到问题的公告进行 for each
循环时:
@if (Auth::user()->activeAnnouncements()->count() > 0)
@foreach(Auth::user()->activeAnnouncements()->announcement as $announcement)
//code to display announcement
@endforeach
@endif
当我链接 ->announcement
时,我 运行 遇到了麻烦。我这样定义关系:
public function announcement()
{
return $this->belongsTo('\App\Announcements');
}
但是我收到错误:
Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$announcement
使用 Tinker 和 运行ning \App\User::find(1)->activeAnnouncements()->first()->announcement
时会显示正确的公告。
尝试将这些加载到我的 foreach 循环中时我哪里出错了?
您实际上从未在 foreach 中检索数据。相反,Laravel 认为您在说 "build this query, don't execute it, then look for a property called 'announcement' on it"。如果您只是将其更改为 get()
调用,它应该可以工作:
@foreach(Auth::user()->activeAnnouncements()->get() as $announcement)
或者更好的是,使用自动 属性 版本:
@foreach(Auth::user()->activeAnnouncements as $announcement)
顺便说一下,您可能想看看 Blade 的 @each
功能:https://laravel.com/docs/5.3/blade#rendering-views-for-collections
在您的 class 用户中,定义一个 announcements
关系,如下所示:
public function announcements() {
return $this->hasManyThrough(Announcement::class,ActiveAnnouncement::class);
}
然后您可以使用 announcement
访问用户的公告:
@if (Auth::user()->announcements()->count() > 0)
@foreach(Auth::user()->announcements as $announcement)
//code to display announcement
@endforeach
@endif
备注:
也许您可以简单地在公告中添加一列 is_active
并删除 ActiveAnnouncement 模型。这样,在 class User
中定义 announcements
关系:
在您的 class 用户中,定义一个 announcements
关系,如下所示:
public function announcements() {
return $this->hasMany(Announcement::class);
}
并通过以下方式访问活动公告:
$activeAnnouncements = $user->announcements()->whereIsActive(true)->get()
希望这会有所帮助