如何更新条件多于 Laravel 中 ID 的模型?

How to update a model with more conditions than an ID in Laravel?

我是 Laravel 5.0 的新人,我试图找到一个 user 来更新他,但有一些条件,比如状态或到期日期。当我使用 find 方法时,只要给定用户 ID 就可以更新,因为我得到一个 App\User 实例,因此可以更新:$User->fill($newData)->save();

但是当我需要设置一些条件来查询时,实例来自Illuminate\Database\Eloquent\Builder并且显然没有填充方法因为不是App\User(不是Eloquent 型号):

$User = \Reverse\Interest::find($id)
    ->where('status', '<>', 'DELETED')
    ->where('expires_at', '>=', DB::raw('NOW()'))

所以,$User->fill($newData)->save()是不可能的...

如何查找具有 ID 和更多条件的用户并获取 App\User 个实例来更新而不是 Illuminate\Database\Eloquent\Builder 个实例?

只需调用 first() 并使用 where('id', ...:

$User = \Reverse\Interest::where('id', $id)
    ->where('status', '<>', 'DELETED')
    ->where('expires_at', '>=', DB::raw('NOW()'))
    ->first();

if($User !== null){
    $User->fill($newData)->save();
}