0

So, I have this:

item.blade.php(locaton: 'pages/components/item.blade.php')

<div>
 // some php logic
test
</div>

list.blade.php

// some laravel blade logic

<script>
    function onAddItem() {

// the code above doesn't work
        $.get("visitor.pages.service.html", function(data){
           $('#appends').append(data);
        });
      }
    }

</script>


<div>
  <button onclick="onAddItem()">add item</button>

  <div id="list">
    @include('pages.components.item', ['someVars'=> false]); //this displays OK
  </div>

</div>

Now what I wanna do is when I click the add item button, it should also add another pages.components.item template inside list (id) div.

I already tried the answers on these posts:

But both answers don't work.

2 Answers 2

1

As the answer pointed you can't do that, as blade is server side processed. There is a workaround however but it's expensive and prone to vulnerabilities: create custom routes which output the desired blade files when needed.

In routes.php:

Route::get('/blades/{path_to_blade}', function($path_to_blade){
    return view($path_to_blade);
})->where('path_to_blade', '^[a-z0-9\_\-\.]+$');

Then you can use an ajax call or whatever to call the requested template. I'm using jQuery as demo:

$.ajax({
    url: "/blades/template.path",
    cache: false,
    success: function(html){
        $("#element_in_which_to_insert").append(html);
    }
});

You can also request the template in browser: /blades/template.path. But again: this method is prone to vulnerabilities. You should use a frontend framework such as Vue or Angular instead.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use

function onAddInvoiceItem()

not

function addItem()

Because you call it with onAddInvoiceItem() here :

<button onclick="onAddInvoiceItem()">add item</button>

1 Comment

oops my bad, but its the same, I just forgot to rename the other one when putting it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.