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

qybbqybb's avatar

Vue resource delete task

Hello Everybody i use the vue resource to delete task in the database i create the deleteTask in the methods

like :

deleteTask:function(task){

            this.list.$remove(task);
            this.$http.delete('api/tasks/'+task.id,task);

}

Router { Route::delete('api/tasks/{id}',function($id){ App\Task::findOrFail($id)->delete(); }); }

But i get the error code 500 and TokenMismatchException in the laravel the instance url s http://54.152.108.125/

So confused

0 likes
3 replies
calebporzio's avatar

Before you initialize the Vue instance you need to include this:

Vue.http.headers.common['X-CSRF-TOKEN'] = <?php echo csrf_token(); ?>;

when you submit a standard form and the csrf middleware is enabled, you have to include the csrf token as a hidden input field. When submitting data via the vue-router, you need to include it in the request header using the above code.

Hope that helps.

qybbqybb's avatar

Should i also put the

meta id="token" name="token" value="{ {csrf_token() }}"

in the head ?

And add the line you suggest in the begining in the main.js? Because i can not insert the php code directly in the js file Would you like to give a instance in detail

TylerODonnell's avatar
Level 28

@qybbqybb Add the CSRF token to your head like this:

<meta id="token" name="token" value="{{ csrf_token() }}">

And fetch in inside your javascript by going something like this

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');
2 likes

Please or to participate in this conversation.