TL; DR:需要每个发件人的最新消息.
在我的Laravel应用程序中,我有两个表:
用户:
消息:
当然还有模特.
用户模型:
public function messages() {
return $this->hasMany('App\Message', 'recipient_id');
}
Run Code Online (Sandbox Code Playgroud)
消息模型:
public function sender() {
return $this->belongsTo('App\User', 'sender_id');
}
public function recipient() {
return $this->belongsTo('App\User', 'recipient_id');
}
Run Code Online (Sandbox Code Playgroud)
当用户打开他的收件箱时,他应该看到来自任何其他用户的最新消息列表.
所以,如果有消息:
id sender_id recipient_id body created_at
1, 2, 1, hi, 2016-06-20 12:00:00
2, 2, 1, hi, 2016-06-21 12:00:00
3, 3, 1, hi, 2016-06-20 12:00:00
4, 3, 1, hi, 2016-06-21 12:00:00
Run Code Online (Sandbox Code Playgroud)
然后,id为1(recipient_id)的用户应该只看到ID为2和4的消息.
这是用户模型中的当前解决方案:
return Message::whereIn('id', function($query) {
$query->selectRaw('max(`id`)')
->from('messages')
->where('recipient_id', '=', …Run Code Online (Sandbox Code Playgroud) 在我的Laravel应用程序中,用户可以禁用(而不是删除)他们的帐户以从网站上消失.但是,如果他们尝试再次登录,则应自动激活他们的帐户,他们应该成功登录.
这是通过users表中的"active"列和User模型中的全局范围完成的:
protected static function boot() {
parent::boot();
static::addGlobalScope('active', function(Builder $builder) {
$builder->where('active', 1);
});
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是那些非活动帐户无法再次登录,因为AuthController找不到它们(超出范围).
我需要实现的目标:
我现在的想法是使用withoutGlobalScope定位用户,手动验证密码,将"active"列更改为1,然后继续常规登录.
在我的authController中使用postLogin方法:
$user = User::withoutGlobalScope('active')
->where('username', $request->username)
->first();
if($user != null) {
if (Hash::check($request->username, $user->password))
{
// Set active column to 1
}
}
return $this->login($request);
Run Code Online (Sandbox Code Playgroud)
所以问题是如何在不改变Laravel主代码的情况下使AuthController忽略全局范围,因此它将保留更新?
谢谢.