If you add a dd('test'); inside the method, is it then shown in a modal?
Inertia.post not working where axios.post does
I have the following controller method that deletes a given record:
public function destroy(Request $request)
{
log::debug("Destroying");
Question::where('id',$request->toDelete)->first()->delete();
return redirect('/questions')->with('message',"Question deleted.");
}
I am trying to trigger this method using the following code:
function deletePrompt(param){
if(confirm("Are you sure you want to delete this question?")){
const data = {toDelete:param};
Inertia.post('/admin/questions/delete',data);
}
}
The problem is that this does nothing and provides no feedback (it just reloads the page. No error message, nothing in console, nothing in laravel.log). "log::debug("Destroying");" also doesn't run. Meanwhile, the following with axios does work fine.
function deletePrompt(param){
if(confirm("Are you sure you want to delete this question?")){
const data = {toDelete:param};
axios.post('/admin/questions/delete',data)
.then((response) => {
console.log(response);
});
}
}
The response logged appears to be the redirect that I want as well. Using axios isn't a good alternative here, because I want Inertia to perform a redirect to an updated version of the page with a flash message. I have using Inertia.post without authentication middleware, but this has not had any effect. Changing the redirect in the controller to inertia(...) or anything else has no effect (nor should it, since the controller seems not to get called). Here is the route file, in case it matters.
Route::middleware(['auth','isAdmin'])->group(function(){
//...
Route::post('/admin/questions/delete',[QuestionController::class,'destroy']);
}
);
Apologies if the problem is something that should be really obvious, but why is Inertia.post(...) doing nothing and how can I fix it?
@PhilKz great to hear. Yeah using a Link component won't work correctly with on click. You could have just replaced it with a regular <button
<button onClick={()=>deletePrompt(question.id)} className='rounded-md bg-red-600 py-1 px-2 mt-1 text-indigo-50'>Delete</button>
Please or to participate in this conversation.