How to refer laravel csrf field inside a vue template

0 votes

I have a vue template that contains a form:

<form id="logout-form" :action="href" method="POST" style="display: none;">
        {{ csrf_field() }}
</form>

In laravel, forms must have a csrf_field() defined. But within a vue component, the statement {{ csrf_field() }} means that I have a method named csrf_field in my vue instance and I am calling it.

How do I add csrf_field under this circumstance?

Oct 21, 2020 in Laravel by kartik
• 37,510 points
2,721 views

1 answer to this question.

0 votes

Hello @kartik,

If you have the token in the meta tag of your header (view)

<meta name="csrf-token" content="{{ csrf_token() }}">

you could access the token using

data() {
    return {
        csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content')
    }
}

And add a hidden input field within the form and bind the csrf property to the value like this:

<form id="logout-form" :action="href" method="POST" style="display: none;">
 
answered Oct 21, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How to make a new page with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
12,012 views
0 votes
1 answer

How to mock a static facade methods in Laravel?

Hey, Facades provide a "static" interface to classes ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,880 points
5,995 views
0 votes
1 answer

How to pass CSRF token with ajax request in Laravel?

Hey, In between head, tag put <meta name="csrf-token" ...READ MORE

answered Mar 24, 2020 in Laravel by Dey
38,454 views
0 votes
1 answer

How to make a constant and use globally in laravel?

Hii, You can create a constants.php page in config folder ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
3,611 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,875 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,678 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,540 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,714 views
0 votes
1 answer

.How to turn off CSRF protection for a particular route in Laravel?

Hey, We can add that particular URL or ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
3,891 views
0 votes
1 answer

How to call a controller function inside a view in laravel 5

Hello @kartik, Just try this in your view ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,880 points
8,008 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP