【问题标题】:Laravel/Sentinel Call to undefined method on routeLaravel/Sentinel 调用路由上未定义的方法
【发布时间】:2017-08-17 15:28:11
【问题描述】:

我正在尝试按照这个很棒的教程构建一个通知系统: https://jplhomer.org/2017/01/building-realtime-chat-app-laravel-5-4-vuejs/

从数据库获取通知工作正常,但是当我尝试通过发布路由保持通知时,我的日志中出现以下错误:

我正在使用 Laravel 5.4、Sentinel、vue2 和 axios

[2017-03-24 15:00:58] local.ERROR: BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::notification() in /Users/odp/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2445

在我的浏览器控制台中,我得到的只是:

Error: Request failed with status code 500

我的get和post路线如下:

// Notifications
Route::get('/notifications', function() {
    return App\Notification::with('user')->get();

});

Route::post('/notifications', function() {
    //$user = Auth::User(); //Original code from tutorial
    Sentinel::getUserRepository()->setModel('App\User');
    $user = Auth::getUser(); //Auth is now an alias for Sentinel

    $user->notification()->create([
    'notification' => request()->get('notification')
    ]);

    return ['status' => 'OK'];

});

app/User.php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Sentinel;

class User extends \Cartalyst\Sentinel\Users\EloquentUser
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'first_name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

public function notifications()
{
    return $this->hasMany(Notification::class);
}
}

app/Notification.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Sentinel; 

class Notification extends Model
{


    protected $fillable = [
        'notification',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

我不知道这是否相关,但这是来自我的 app.js,我相信 axios.post 行会根据失败的路由创建 500 错误。

const app = new Vue({
el: '#app',

data: {

    notifications: []
},

methods: {

    addNotification(notification) {
        this.notifications.push(notification);

        axios.post('/notifications', notification).then(response => {

        })
    } 
},

created() {
    axios.get('/notifications').then(response => {
        //console.log(response);
        this.notifications = response.data;
    });
}
});

【问题讨论】:

    标签: php laravel vue.js axios sentinel


    【解决方案1】:

    错误是它试图在 Illuminate\Database\Query\Builder 对象上调用 notification

    这让我相信 Auth::getUser() 实际上返回的是 Builder 对象,而不是您的实际用户模型。

    我会尝试将 ->get() 添加到 $user = Auth::getUser()->get()

    【讨论】:

    • 我再次执行了扩展 Sentinel 的步骤,发现我错过了一点:github.com/cartalyst/sentinel/wiki/Extending-Sentinel#runtime 我现在已将 Sentinel::getUserRepository()->setModel('App\User'); 添加到我的路线中,以便我可以调用 Sentinel::getUser( );获取用户模型。但错误是一样的。我已经更新了我的问题以反映变化
    • 尝试运行 dd(get_class($user)); 来验证类 ic 是否正确。如果您收到相同的确切错误,则表示 $user 不是您的 User 模型的实例。
    • 另外,当你这样做时:$user->notification() 你是在尝试创建 Laravel 通知还是 App\Notification?在您的用户模型中,您调用了函数notifications,最后带有S,但您调用的notification 没有S
    • 我的天哪。我多么愚蠢。是的,我在没有 S 的情况下调用它,它需要一个 S。不敢相信我没有看到,一定是盯着自己瞎了。在这里,我专注于必须是 Sentinel 给我问题。感谢所有伟大的反馈布赖恩。
    猜你喜欢
    • 2018-06-30
    • 2018-12-16
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多