gandalf
gandalf

Reputation: 118

Laravel: use blade template in javascript

I am using Laravel 5.3 for my application. On a form, I am trying to make ajax call from javascript.

The jquery docs show how to give a static url for the ajax call.

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

I know I can substitute the test.html with any URI from my routes.php file. But I don't want it to be static. It would be better if I can call a Controller function. This will allow me to change the URI in future without changing the code.

In short, I need a way to call the action() function of Laravel in the javascript. How can I achieve this?

Upvotes: 1

Views: 804

Answers (1)

linuxartisan
linuxartisan

Reputation: 2426

It's simple.

Just do this.

$.ajax({
  url: {{ action('ControllerClass@functionName', [parameter_list]) }},
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

Let the blade engine handle the rest of the task.

Upvotes: 1

Related Questions