Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dleroari's avatar

Increment value when a button is triggered

So I have two buttons, one is next and the other is prev. My objective is to increment the value everytime the button is clicked.. I guess after this I should just sell groceries or something, maybe I need some sleep, idk!...

Anyway, I would love to know how to do this..

Blade file


    href="/?date=next"
    href="/?date=prev"

Controller file


    public function show(Hour $post){
        if(request()->has('date')){
            if(request('date') == 'next'){
                $this->count++;
                dd($this->count); // This value is not incrementing  
            }
        }
    }

0 likes
12 replies
shez1983's avatar

if you want just that button (or selected) buttons then do it in the controller like you have done..

an alternative would be to use javascript to send an ajax call..

Snapey's avatar

you are not saving the new count anywhere? so when the next page loads it does not know it was incremented previously

dleroari's avatar

@Snapey, I added a property protected $count = 0; in the same class. Usually that works in other coding langauges when the function is called..

Where should I save the value so I can use the increment?

Snapey's avatar

Classes only live as long as the current request cycle. It does not matter if you stored a value in the class, next time you press the button you are in a completely new request cycle and with a new copy of the class again.

If for some reason you cannot use the frameworks paginator then you will need something like a page counter that is included in every request and incremented or decremented by the view.

So first page then your URL looks like /mypage?page=1 and you have a next button which has the url /mypage?page=2 then on page 2, a prev button which has /mypage?page=1 and a next button = /mypage?page=3

This is what the pagination class will do for you automatically.

dleroari's avatar

@Snapey, thank you. But I do not want to use paginate in this scenario. There must be a way I can increment the value each time the user clicks the button.

What I am essentially trying to achieve is to show the user the rows for the next month.

And when that query parameter href=/?date=next, I run this method $post->date->nextMonth().

However, when the function is triggered, it does not update to the next month. Therefore I thought I just might add a value that increments to achieve this, like $post->date->nextMonth($count).

Any other alternatives?

Snapey's avatar

You probably have to persist the 'current' value in the querystring.

You can use the session to store it but this breaks if the user opens two tabs for the same page since they will share the session

biishmar's avatar

html

<a href="/?date=next" data-count=1 > 

javascript

$('a').click(function() {
count = $(this).attr('data-count');

href = location.href + '&count=' + count;

location.href = href;
});

php

public function show(Hour $post){
        if(request()->has('date')){
            if(request('date') == 'next'){
                dd(request('count')); // This value is from javascript
            }
        }
    }

1 like
Snapey's avatar

(assuming php 7)

    public function show(Hour $post){

    $date = request()->date ?? 0;


    return view('your.view.name.here')->with(compact('date'));

    }

view


@if($date>0)

    <a href='/?date={{ $date-1 }}'>Previous</a>

@endif 

    <a href='/?date={{ $date+1 }}'>Next</a>

1 like
dleroari's avatar
dleroari
OP
Best Answer
Level 2

Thanks for the feedback, well appreciated! But I find the solution on StackOverflow to solve my problem in a convenient and simple way.

Controller file:

public function show(Hour $post){
     $months = request('months', 0);

    if (request('date') === 'next'){
        $posts = $post->date->addMonth();
        $months++;
    }elseif(request('date') === 'prev'){
        $posts = $post->date->subMonth();
        $months--;
    }

     return view('user.table', compact('posts', 'months')); 
}

The view:


   class="button is-primary is-outlined" href="/?date=next&months={{ $months    }}">Previous
   class="button is-primary is-outlined" href="/?date=next&months={{ $months }}">Next

Link to stackoverflow solution: https://stackoverflow.com/questions/48203080/next-prev-month-is-not-working-in-laravel/48203322#48203322

Snapey's avatar

its basically the same but you are sending an extra parameter to determine if next or prev was pushed, wheras I set the button value in the view.

The first line in your view is wrong as you send 'next' for both buttons

1 like

Please or to participate in this conversation.