我有3张桌子作为图片,
site_notifications
site_users
site__users__notifactions
我想建立关系,以便显示特定用户的通知详细信息
我尝试过
public function notification()
{
return $this->hasManyThrough('App\SiteNotification', 'App\Site_Users_Notifaction', 'id', 'user_id');
}
但是它给了我空
答案 0 :(得分:0)
这不是那样。您应该使用Many To Many Relationships
您的表格名称应为:
您的型号名称应为:
然后在User
类中:
class User extends Model
{
public function notifactions()
{
return $this->belongsToMany(Notifaction::class);
}
}
然后在Notifaction
类中:
class Notifaction extends Model
{
public function users()
{
return $this->belongsToMany(User::class);
}
}
然后您可以像这样查询:
$user = App\User::find(1);
foreach ($user->notifactions as $notifaction) {
//
}