V
V
Valera Udav2016-07-04 13:57:19
JavaScript
Valera Udav, 2016-07-04 13:57:19

How to use JavaScript inside PHP in Laravel?

The task is the following: count the number of elements in JS and display the number with the correct declension (eg "1 order / 2 orders / 5 orders") using Laravel lang files.
At first, I used a simple JS function for declensions, and simply stored three different declensions in lang files. After that, I was told that it is wrong to do this and there is a standard function in Laravel that does this trans_choice. But there was a problem when integrating the JS variable into the PHP code of this function. It turned out something like this (in a simplified form):

// определяем количество карточек в колонке
var cardsLength = 3;
// и записываем
countText = cardsLength+' {{ trans_choice("sales_order.order_declension", '+cardsLength+', [], "ru") }}';

Of course, the code did not work adequately and always displayed the third declension option. The problem obviously arises from the fact that the php code does not see the value of the JS variable, but perceives it as plain text.
Unfortunately, I could not think of any working options (well, except for a rather ill-conceived option using the JS declination function). Who can suggest the most elegant solution to the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pochepko, 2016-07-04
@DJZT

At the time of rendering the page, Js has not yet been worked out.
Still, I advise you a way to solve this in a standard way.
PS Write in more detail what you are doing and what you want to achieve as a result - and I will try to tell you how to do it more elegantly using PHP + Js environments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question