I want to toggle publish stat of my exams in laravel, I created a route for toggling and it worked, but I don't want to refresh the page every time I send a POST request so I created this function to send,
function togglePublish(id){
let token = {{ csrf_token() }};
$.ajax({
type: "post",
url: `{{ route('togglePublish','${id}') }}`,
data: `_token=${token}`
});
}
I meant by this after the laravel compile the page, the URL will be https://localhost/toggle/${id}
then javascript change the id variable to 1
for example, so the link be https://localhost/toggle/1
and that will work, but laravel returned
Missing required parameter for [Route: togglePublish] [URI: a/togglePublish/{exam}] [Missing parameter: id].
I think I have to add @csrf
the route
Route::post('/a/togglePublish/{exam:id}', [AdminExamController::class, 'togglePublish'])->name('togglePublish');
the togglePublish
function in AdminExamController
controller
public function togglePublish(Exam $exam)
{
dd($exam);
}