Php Laravel/Sentinel对路由上未定义方法的调用
我正试图根据本教程构建一个通知系统: 从DB获取通知工作正常,但当我尝试通过post路由使通知保持时,我的日志中出现以下错误: 我使用的是Laravel 5.4、Sentinel、vue2和axiosPhp Laravel/Sentinel对路由上未定义方法的调用,php,laravel,vue.js,axios,sentinel,Php,Laravel,Vue.js,Axios,Sentinel,我正试图根据本教程构建一个通知系统: 从DB获取通知工作正常,但当我尝试通过post路由使通知保持时,我的日志中出现以下错误: 我使用的是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/
[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'];
});
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);
}
}
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/Notification.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);
}
}
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;
});
}
});
错误是它试图调用
对象上的通知
这让我相信Auth::getUser()
实际上返回的是Builder
对象,而不是实际的用户模型
我会尝试将->get()
添加到$user=Auth::getUser()->get()
我又一次运行了扩展Sentinel的步骤,发现我遗漏了一点:我现在添加了Sentinel::getUserRepository()->setModel('App\user')编码>到我的路由,这样我就可以调用Sentinel::getUser();获取用户模型。但错误是一样的。我已经更新了我的问题,以反映更改尝试并运行dd(get_class($user))代码>以验证类ic是否正确。如果您遇到相同的错误,则意味着$user
不是您的用户模型的实例。此外,当您执行此操作时:$user->notification()
是否正在尝试创建Laravel通知或App\通知?在您的用户模型中,您调用函数notifications
时最后使用了S
,但是您调用的是notification
,而没有使用S
。哦,我的上帝。我有多傻。是的,我叫它没有S,它需要S。真不敢相信我没有看到,一定是我自己瞎了眼。在这里,我专注于它必须是哨兵给我的问题。谢谢你的反馈,布莱恩。