如何使用 Livewire 制作订阅和取消订阅按钮?

How can I make subscribe and unsubscribe button with Livewire?

我有两个按钮subscribe and unsubscribe我需要在用户点击subscribe时将按钮实时更改为unsubscribe

我做了 subscribe 一个,但我不知道如何切换到 unsubscribe

public function subscribe()
{
    Subscribe::firstOrCreate([
        'user_id' =>  user()->id,
        'store_id' => $this->store->id,
    ]);
    
}

HTML

 <button wire:click="subscribe" class="btn btn-default">
    <i class="far fa-heart text-danger h4 pt-2"></i>
</button>

我会在组件上创建一个 public(计算)属性,其中包含您需要确定用户是否已订阅的任何逻辑:

public function getIsSubscribedProperty()
{
    // return true or false from here
}

然后您可以将按钮包裹在 blade 'if':

@if($isSubscribed)

<button wire:click="unsubscribe" class="btn btn-default">
    <i class="far fa-times-circle text-danger h4 pt-2"></i>
</button>

@else

<button wire:click="subscribe" class="btn btn-default">
    <i class="far fa-heart text-danger h4 pt-2"></i>
</button>

@endif