Home > Enterprise >  how do I pass a variable from js file to laravel controller to make a query based on the variable ta
how do I pass a variable from js file to laravel controller to make a query based on the variable ta

Time:12-09

this is code in laravel controller

$data = table::where('field',$var)->get();

this is the js file im trying to receive the variable from to pass it to the query

function clicked(var)
{
    console.log(var);
}

CodePudding user response:

The Front end will need to send data to the backend via a HTTP request. This can be done via AJAX. There are good libraries that you can use to do this such as AXIOS if you are using Laravel with Vue.JS. Here is an example of how to use it:

axios.post('/URL_HERE', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
  • Related