0

I'm using Laravel Framwork and try to use a javascript viarable inside a php echo call but it doesn't work. Do you have any solution?

function getMachineName (id) {
        return item = {{ Machine:: find(id)->name }};
    }

3 Answers3

1

JavaScript variables can not be used within PHP . you need to use Ajax for this purpose.

Adeel Raza
  • 628
  • 5
  • 17
0

In a straight up way you can't use the variables
But what you can do is after the function is done you can redirect to the same page and put the variable as a parameter in the URL and access it from there.
Though as suggested ajax would be better for that.

Yousef_Shamshoum
  • 787
  • 3
  • 12
0

Depending on what you're trying to do and the number of Machines there are in your database, you could do this in your controller:

$machines = Machine::all();

And something like this in your view:

var machines = {{ $machines }};

function getMachineName (id) {
    for (i = 0; i < machines.length ; i++) {
        if (machines[i].id === id) {
            return machines[i].name;
    }
}