Skip to content
Advertisement

how do I pass a variable from js file to laravel controller to make a query based on the variable taken from an element clicked

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);
}

Advertisement

Answer

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);
  });
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement