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

Yorkata's avatar

Update on click

Trying to update status for a specific record in my table when a button is clicked. Controller function:

public function push(Request $request, Competition $competition)
        {    
            $competition->update(['status' => 'to_push']);
            $competition->save();
        }

The new button:

<a class="btn btn-dark btn-sm me-1" href="{{ route('competitions.index',$competition->id) }}">Push</a>

It only refreshes the page and does not change the value. What am I doing wrong?

0 likes
9 replies
Yorkata's avatar

@Sinnbeck I don't have a specific route dedicated to this push function. I am loading the index like this:

Route::resource('competitions', CompetitionController::class);

In there I am showing the results from my table and, ideally, when I click update only one record status will change without changing the URL or the view

Sinnbeck's avatar

@Yorkata You need a route for it

Route::get('competitions/push/{competition}', [CompetitionController::class, 'push'])->name('competitions.push');

//and
<a class="btn btn-dark btn-sm me-1" href="{{ route('competitions.push',$competition->id) }}">Push</a>
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

But you probably want to use a POST or PUT route and a form instead of this, so you can have csrf

Route::put('competitions/push/{competition}', [CompetitionController::class, 'push'])->name('competitions.push');

//and
<form action="{{ route('competitions.push',$competition->id) }}" method="post">
<button class="btn btn-dark btn-sm me-1">Push</button>
@csrf
@method('put')
</form>

and you want to send the user back to the page afterwards

public function push(Request $request, Competition $competition)
        {    
            $competition->update(['status' => 'to_push']);
            $competition->save();
            return back(); //go back
        }
1 like
Tray2's avatar

@Yorkata Like @sinnbeck says, you should use a form and a post/put/patch request for any update of your database.

Sinnbeck's avatar

@Yorkata Your own code was an <a href tag. Even if it is inside a form, it does not use csrf.

Please or to participate in this conversation.