0

I'm trying to pass a JS variable via .innerHTML method into the blade command @include in Laravel and Laravel won't have anything to do with it.

Here is my JS function:

<script type="application/javascript" >
function itemFeatures(divId, subCatId){
    axios.get('/item-type/' + subCatId.value)
   .then(function (response){
   let itemName = response.data[0].name;
document.getElementById(divId).innerHTML = `@include('gearitem.itemtype.${itemName}')`    
    })    
}
</script>

I have a blade who's address is gearitem.itemtype.tent.blade.php for example and the 'response.data[0].name' yields 'tent' so logically i'd expect the variable itemName to place 'tent' at the appropriate place in the @include string but instead I'm getting the following error from Laravel:

gearitem.itemtype.${itemName} was not found. Are you sure the view exists and is a .blade.php file?

I have tested the blade by hard coding it so = @include('gearitem.itemtype.tent') inside the div without a problem (it appears correctly) I also tried an .innerHTML = <p>${itemName}</p>and the string 'tent' appears in the div perfectly. It seems like Laravel is meddling with my syntax inside the JS before it is even rendered for some reason... What am i missing here?

nurge
  • 99
  • 9
  • This won't work. PHP gets processed before the page gets submitted to the screen, JS gets processed after. You cannot pass data to PHP from JS like this. – aynber Jun 02 '20 at 15:42
  • What I want to do is get a different blade partial as a function of what the user is choosing. How could I get the correct blade file by feeding its path with the user's choice? I.e if the user chooses 'tent' i want to give him the gearitem.itemtype.tent blade but if he chooses 'backpack' i want to give him the gearitem.itemtype.backpack blade how can I do it?? – nurge Jun 02 '20 at 15:50
  • You're probably going to have to use your ajax call to send back the generated HTML from the blade. I think there's a way to return the view or use `__toString()`, but I couldn't tell you off-hand how to do that. – aynber Jun 02 '20 at 16:07

0 Answers0