我正在尝试按照这个很棒的教程构建一个通知系统: 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
我的到达和发布路线如下:
// 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'];
});
应用/ 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);
}
}
应用/ 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;
});
}
});
答案 0 :(得分:0)
错误在于它试图在notification
对象上调用Illuminate\Database\Query\Builder
。
这让我相信Auth::getUser()
实际上正在返回一个Builder
对象,而不是您的实际用户模型。
我会尝试将->get()
添加到$user = Auth::getUser()->get()
。