Disable the button after the click, and change it's value

Viewed 47

I have a Laravel project, where I allow people to subscribe to specific campaigns. There is a button "Subscribe", and once pressed, I write the information in the pivot table.

The relation between User and Campaign is belongsToMany both ways.

The question is, how could I disable such button, so client's couldn't subscribe to the same campaign many times? In my thoughts, after the button click, it would get disabled, and would show "Subscribed".

Current button:

  <x-button>
<a href="subscribes/{{$item->id}}" class="btn btn-dang btn-xs" id="subscribeButton">Subscribe</a>
</x-button>

EDIT:

So I had an idea, to check if this specific combination of campaign_id and user_id exists within the pivot table, and get a boolean.

Controller

    public static function button(Request $request)
{
    $campaignUser = User::find($request->user()->id);
    $hasCampaign = $campaignUser->campaigns()->where('id', $request->id)->exists();
    return $hasCampaign;
}

I do receive a proper response, either 1 or something else.

Now I am trying to call it from the view (still checking if it would work):

    @if(App\Http\Controllers\SubscribeController::button() == 1)
echo "Yes";
@else
echo "No";
@endif

The current issue, is that of course, I need to somehow pass the Request $request into the function, though, not sure if it is even possible.

If it would work like that, I would try to adjust my button code by example:

<input type="hidden" name="status" value="{{ $post->status == 1 ? 0 : 1 }}"/>
@if($post->status == 1)
    <input type="submit" value="Subscribed"/>
@else
    <input type="submit" value="Subscribe"/>
@endif 

So the current question, is if I am even doing it correctly, and if so, how to pass the Request into the function?

0 Answers
Related