Rowayda Khayri
Rowayda Khayri

Reputation: 489

Use Foreach Loop in Javascript with Laravel 5.3

I want to make a button to add new item row :

enter image description here

This is the HTML in create.blade.php

            <div class="createOrderForm" id="orderMenuItems">
            
                <div class="orderItem">
                    
                    <label> Item : </label>
                    <select name="item[]" required>

                        <option value="">Menu Items</option>

                        @foreach ($menuItems as $item)
                        <option value="{{$item->id}}">{{$item->name}}</option>
                        @endforeach

                    </select>
                    
                    <label>Quantity :</label>
                    
                    <input  type="text" name="quantity[]" value="" required>
                
                </div>
            
            </div>

and this is create.js that's included :

$(document).ready(function () {
console.log("Welcome To create order Page");


var addItem = $('#addItem');
var orderMenuItems = $('#orderMenuItems');



$(addItem).click(function(e){
   
    var newItem = '<div class="orderItem">';
                    
    newItem += '<label> Item : </label>';

    newItem += '<select name="orderItem[]">';

    newItem += '<option value="">Menu Items</option>';

    newItem += "@foreach ($menuItems as $item)";

    var itemID = "{!! $item->id !!}";
    
    newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';

    newItem += "@endforeach";

    newItem += '</select>';
    
    newItem += '<label>Quantity :</label>';
    
    newItem += '<input  type="text" name="quantity[]" value="" required>';
                
    newItem += '</div>';
    
    $(orderMenuItems).append(newItem);
                
});
});

The button works and adds new row, but there is a problem with the menu items in the new row :

enter image description here

I think the problem is here :

newItem += "@foreach ($menuItems as $item)";

    var itemID = "{!! $item->id !!}";
    
    newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';

    newItem += "@endforeach";

I tried to use :

@foreach ($menuItems as $item)
....
@endforeach

instead of :

newItem += "@foreach ($menuItems as $item)";
....
newItem += "@endforeach";

but it doesn't work.

How Can I fix it ??

Upvotes: 2

Views: 12076

Answers (1)

Thomas
Thomas

Reputation: 12637

I have no experience in laravel, but I'd guess that it simply doesn't feel responsible for js files.

I don't like this approach where you store the markup in two places. Once in the html, and once in a String in JS. And you have to keep these two places sync.

May I suggest a different approach:

in create.blade.php:

<div class="createOrderForm" id="orderMenuItems">
    <div class="orderItem">

        <label> Item : </label>
        <select name="item[]" required>

            <option value="">Menu Items</option>

            @foreach ($menuItems as $item)
            <option value="{{$item->id}}">{{$item->name}}</option>
            @endforeach

        </select>

        <label>Quantity :</label>

        <input  type="text" name="quantity[]" value="" required>

    </div>
</div>

the template can be anywhere in the page, so why not store it right next to where it's used

and in create.js

$(document).ready(function () {
    console.log("Welcome To create order Page");

    var $addItem = $('#addItem');
    var $orderMenuItems = $('#orderMenuItems');

    $addItem.click(function(){
        var markup = $orderMenuItems.children('.orderItem')[0].outerHTML;
        $orderMenuItems.append(markup);
    });
});

Upvotes: 2

Related Questions