Laravel 论坛系统里消息提醒里不明白的代码?

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class NotificationsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(){
        //获取当前登陆的用户的所有通知
        $notifications = Auth::user()->notifications()->paginate(20);

        return view('notifications.index',compact('notifications'));
    }
}

这里面的Auth::user()->notifications()哪里出来的哦,Auth::user()->下面怎么就出来个nitification()的方法呢,怎么来的一直没找到?谁帮看下好么

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

这里的 Auth::user() 返回的是User 模型,在User模型中引入了 use Notifiable这个trait,这里有notifications的方法

6年前 评论
讨论数量: 2
lmaster

额,不知道你的怎么弄,

use Illuminate\Support\Facades\Auth;

去看了下类

class Auth extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'auth';
    }
    public static function routes()
    {
        static::$app->make('router')->auth();
    }
}

没有那就去父类

abstract class Facade
{
     .
     .
     .
    /**
     * Get the root object behind the facade.
     *
     * @return mixed
     */
    public static function getFacadeRoot()
    {
        return static::resolveFacadeInstance(static::getFacadeAccessor());
    }

    /**
     * Get the registered name of the component.
     *
     * @return string
     *
     * @throws \RuntimeException
     */
    protected static function getFacadeAccessor()
    {
        throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
    }

    /**
     * Resolve the facade root instance from the container.
     *
     * @param  string|object  $name
     * @return mixed
     */
    protected static function resolveFacadeInstance($name)
    {
        if (is_object($name)) {
            return $name;
        }

        if (isset(static::$resolvedInstance[$name])) {
            return static::$resolvedInstance[$name];
        }

        return static::$resolvedInstance[$name] = static::$app[$name];
    }
     .
     .
     .
    public static function __callStatic($method, $args)
    {
        $instance = static::getFacadeRoot();// 返回解析实例 Auth

        if (! $instance) {
            throw new RuntimeException('A facade root has not been set.');
        }

        return $instance->$method(...$args);
    }
}

Auth::user() 走的就是 public static function __callStatic($method, $args),在往下我没有代码,就不知道怎么追踪了,你可以在
$instance = static::getFacadeRoot(); 下面打 $instance 打出来,var_dump();dd();或者 \Log::info();等方式看下这个是什么,在去找

6年前 评论
Epona

这里的 Auth::user() 返回的是User 模型,在User模型中引入了 use Notifiable这个trait,这里有notifications的方法

6年前 评论

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