如何在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();
}