-1

i´m trying to make a condition via ajax in my code wich show a button when user is 'admin', simple in blade, but it doesn´t work printing from jquery.

-- js code --

codigo += '<tr>...<td class=""><input name="incertidumbre'+[i]+'" class="pl-2 font-weight-bolder sinbordeimpar" value="'+puntos[i].incertidumbre+'" disabled></td>';
        codigo += @if(Auth::user() && Auth::user()->rol == "admin")
        codigo += '<td><input class="btn btn-sm btn-outline-secondary" type="button" value="Editar" name="btnedit_Punto'+puntos[i].id+'" id="btnGuardar_Punto'+puntos[i].id+'">';
        codigo += '<input hidden="hidden" class="btn btn-sm btn-outline-secondary" type="button" value="Guardar" name="btnedit_Punto'+puntos[i].id+'" id="btnGuardar_Punto'+puntos[i].id+'"></td>';
        codigo += @endif
        codigo += '</tr>'

}

      $('#prueba').html(codigo);
    };

result is "Uncaught SyntaxError: Invalid or unexpected token"

How should i send "var codigo" to blade view for a correct condition in it??

Oscar_co
  • 1
  • 2
  • 1
    Blade templates are parsed in PHP on the server, before they are sent to the client (the browser). The browser can't parse blade syntax so you can't dynamically add blade syntax using JS like this. – M. Eriksson May 30 '20 at 11:24
  • You can store Auth::user() and Auth->user() - >role in 2 hidden inputs so that you can access them from JS. Or if there's an Ajax call to the server you can deal with that condition on server side and get the proper response back to your page. – nikistag May 30 '20 at 11:38
  • thanks, i´m going to try storing it in a hidden input!! – Oscar_co May 30 '20 at 13:31

1 Answers1

0

There is solution you can use. Change your js file extension to be blade.php. The rest of the content can be the same, remember to add in <?php Then in your javascript code portion, you can now assign whatever you want to be variable such as const isAdmin = {{Auth::user()->rol=='admin'}}. Also, bear in mind that you have to use @include now instead of adding as ascript

Edward Chew
  • 492
  • 3
  • 12