user3814183
user3814183

Reputation: 1

How to use Javascript viariables in a php echo (Laravel)

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

Upvotes: 0

Views: 96

Answers (3)

user347284
user347284

Reputation:

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

Upvotes: 0

Yousef_Shamshoum
Yousef_Shamshoum

Reputation: 822

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.

Upvotes: 0

Adeel Raza
Adeel Raza

Reputation: 634

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

Upvotes: 1

Related Questions