请教一下 user 和 notifications 的关联是在什么时候建立的?

在 新建 NotificationsController 中:

public function index()
    {
        // 获取登录用户的所有通知
        $notifications = Auth::user()->notifications()->paginate(20);
        return view('notifications.index', compact('notifications'));
    }

这里面的 Auth::user()->notifications() 之间的关联是什么时候创建的呢?
打印出来是多态关联(MorphMany),但是在 User 模型中好像并没有建立 相应的方法 类似 belongsTo、hasMany 之类的

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

引入关系: User模型 <---- use Illuminate\Notifications\Notifiable; <---- use HasDatabaseNotifications
notifications()方法就在HasDatabaseNotifications(位置:vendor/laravel/framework/src/Illuminate/Notifications)这个trait中:

public function notifications()
{
      return $this->morphMany(DatabaseNotification::class, 'notifiable')->orderBy('created_at', 'desc');
}

这里定义了多态关系。

5年前 评论
讨论数量: 1

引入关系: User模型 <---- use Illuminate\Notifications\Notifiable; <---- use HasDatabaseNotifications
notifications()方法就在HasDatabaseNotifications(位置:vendor/laravel/framework/src/Illuminate/Notifications)这个trait中:

public function notifications()
{
      return $this->morphMany(DatabaseNotification::class, 'notifiable')->orderBy('created_at', 'desc');
}

这里定义了多态关系。

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!