简体   繁体   English

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

[英]How can I make subscribe and unsubscribe button with Livewire?

I have two buttons subscribe and unsubscribe I need when the user click on subscribe change the button to unsubscribe real-time:我有两个按钮subscribe and unsubscribe当用户单击subscribe时我需要将按钮更改为实时unsubscribe

I do subscribe one but I don't know how can I switch to unsubscribe我确实subscribe了一个,但我不知道如何切换到unsubscribe

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

HTML HTML

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

I would make a public (computed) property on the component that contains whatever logic you need to determine whether the user is subscribed:我会在组件上创建一个公共(计算)属性,其中包含确定用户是否订阅所需的任何逻辑:

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

then you can wrap your buttons in a blade 'if':然后你可以将你的按钮包裹在刀片'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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM