0

I want to call laravel helper function in javascript. I don't know whether it is possible or not but i have tried by this way but failed

function checkout(){
 var cartArray = shoppingCart.listCart();
 var output = "";
 for(var i in cartArray) {
    output += "<tr class='rem3'>"
             + "<td class='invert'> " + resturant(cartArray[i].resturant)  + " </td>"
             + "</tr>";
     $('#checkouttable').html(output);
}

Laravel helper function

if(!function_exists('resturant'))
{
    function resturant($id)
    {
        $resturant = Resturant::find($id)->restaurant_name;
        return $resturant;
    }
}

Error Uncaught ReferenceError: resturant is not defined

  • 1
    If you want to invoke a server-side PHP function from the client-side JavaScript code then the technology you're looking for is called: AJAX. Your favorite search engine can help you find tutorials and examples on how to use AJAX with Laravel. – David Jun 30 '20 at 17:09
  • Yeah, you can't. `resturant()` is a PHP function, but `cartArray` is a JS variable. Since PHP processes long before JS does, it has no idea what `cartArray` is. After that, JS has no idea what `resturant()` is, etc etc. As stated, an AJAX request to call the PHP function, while sending the JS variable is the approach you're looking for. – Tim Lewis Jun 30 '20 at 17:10

0 Answers0