【发布时间】: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