Ad

Laravel For Loop Error

- 1 answer

When I receive a number in my controller and pass it to the view like this:

public function show(Request $request)
{
    $products = $request->products;
    return view('admin.administration')->with('products',$products);
}

And show it in my view like this:

<div class="form-group">
                        @for($i=0; $i <{{ $products }}; $i++)
                            <div class="form-group">
                            <label for="rol" class="col-lg-2 control-label">Product:</label>
                               <div class="col-lg-10">
                                    <input type="text" class="form-control" id="title>" placeholder="Titel" name="title>">
                                    <input type="text" class="form-control" id="description>" placeholder="Beschrijving" name="description>">
                                    <input type="text" class="form-control" id="price>" placeholder="prijs" name="price">
                                    <input type="text" class="form-control" id="number>" placeholder="Aantal" name="number">
                                    <input type="text" class="form-control" id="total>" placeholder="Totaal" name="total">
                                </div>
                             </div>
                        @endfor
                    </div>

It throws the following error:

Parse error: syntax error, unexpected '?' (View: /home/vagrant/Code/l/resources/views/admin/administration.blade.php)

Checked with a dd but the value isn't empty. When I replace {{ $products }} with for example 5 it works. What should I do?

Ad

Answer

Try replacing

@for($i=0; $i < {{ $products}}; $i++)

with

@for($i=0; $i < $products; $i++)

{{$products}} is shorthand for <?php echo $products; ?>

Ad
source: stackoverflow.com
Ad