【发布时间】:2019-09-09 02:34:31
【问题描述】:
我遇到了软删除问题。我的应用中有一个功能,用户可以在其中为关注的房地产广告加注星标。他们还可以取消为房地产广告加星标。
这很好用。当他们取消星号时,该记录被软删除。 delete_at 时间戳已更新。
但是,如果用户再次尝试为它加星标,我会收到一条消息,指出该属性已被点赞/加星标。那么软删除被忽略了吗?有什么想法吗?
StarredPropertyModel
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class StarredProperty extends Model
{
use SoftDeletes;
protected $fillable = ['property_id', 'user_id'];
public function scopeStarredProperty($query, $propertyId, $userId)
{
return $query->where('property_id', $propertyId)->where('user_id', $userId)->first();
}
}
StarredPropertyController
class StarredPropertyController extends Controller
{
public function star(Property $property, User $user, Request $request)
{
if(!$user->starredProperties()->starredProperty($property->id, $user->id))
{
return response()->json(StarredProperty::create(['property_id' => $property->id, 'user_id' => $user->id]));
}
return response()->json('You have already like this property');
}
public function unstar(Property $property, User $user, Request $request)
{
$starredProperty = $user->starredProperties()->starredProperty($property->id, $user->id);
if($starredProperty->exists())
{
$starredProperty->delete();
}
}
}
【问题讨论】:
标签: php laravel laravel-5 eloquent laravel-5.8