Skip to content
Advertisement

Laravel: Undefined variable in @foreach

I want to display order_details table on web, but @foreach loop says Undefined variable $order_details Here is my @foreach loop

@foreach($order_details as $order_detail)
<tr>
<td>{{$order_detail->product_id}}</td>
<td>{{$order_detail->name}}</td>
<td>{{$order_detail->phone}}</td>
</tr>
@endforeach

My order controller contains this:

   public function index()
{
$order_details=Order_Detail::all();
return view('orders.index',['order_details' => $order_details]);
}

index.blade.php contains only css and javascript code. my index.blade.php is further connected to livewire(order.blade.php) like this

@livewire(‘order’)

my livewire(order.blade.php) contains this code

<div class="col-lg-12">
        <div class="row">
            <div class="col-md-8">
            <div class="card">

            <div class="card-header"><h4 style="float:left">Order Products</h4>
                <a href="#" style="float:right" class="btn btn-dark" 
                data-toggle="modal" data-target="#addproduct">
                <i class="fa fa-plus"></i>Add new Product </a></div>

              <div class="card-body">
              <div class="my-2">
              <form wire:submit.prevent="InsertoCart">
                <input type="text" name="" wire:model="product_code"
                  id="" class="form-control" placeholder="Enter Product code">
                  </form>
              </div>
              @if(session()->has('success'))
              <div class="alert alert-success">
                {{session('success')}}
              </div>
              @elseif(session()->has('info'))
              <div class="alert alert-info">
                {{session('info')}}
              </div>
              @elseif(session()->has('error'))
              <div class="alert alert-danger">
                {{session('error')}}
              </div>
              @endif

                <Table class="table table-bordered table-left">
                  <thead>
                  <tr>
                  <th></th>
                  <th>Product Name</th>
                  <th>Qty</th>
                  <th>Price</th>
                  <th>Discount (%)</th>
                  <th colspan="6">Total</th>
                  </tr>
                  </thead>
                  <tbody class="addMoreProduct">

                    @foreach($productIncart as $key=> $cart)
                    <tr>
                      <td class="no">{{$key + 1}}</td>
                      <td>
                        <input type="text" class="form-control" value="{{$cart->product->product_name}}">
                      </td>
                      <td width="15%">
                        <div class="row">
                        <div class="col-md-2">
                          <button wire:click.prevent="IncrementQty({{$cart->id}})"
                          class="btn btn-sm btn-success"> + </button>
                        </div>
                        <div class="col-md-1">
                        <label for="">{{$cart->product_qty}}</label>
                        </div>
                        <div class="col-md-2">
                        <button wire:click.prevent="DecrementQty({{$cart->id}})"
                          class="btn btn-sm btn-danger"> - </button>
                        </div>
                      </div>
                    </td>
                    <td>
                        <input type="number"
                        value="{{$cart->product->price}}" class="form-control">
                    </td>
                    <td>
                        <input type="number"
                        class="form-control">
                    </td>
                    <td>
                        <input type="number" 
                        value="{{$cart->product_qty * $cart->product->price}}"
                        class="form-control total_amount">
                    </td>
                    <td><a href="#" class="btn btn-sm btn-danger rounded-circle">
                        <i class="fa fa-times" wire:click="removeProduct({{$cart->id}})"></i>
                    </a></td>
                    </tr>
                @endforeach

                    </tbody>              
                </table>


              </div>
            </div>     
          </div>


    <div class="col-md-4">
        <div class="card">
    <div class="card-header">
            <h4>Total <b class="total1">{{$productIncart->sum('product_price')}}</b></h4>
          </div>

          <form action="{{route('orders.store')}}" method="POST">
            @csrf
            @foreach($productIncart as $key=> $cart)
                        <input type="hidden" class="form-control" name="product_id[]" value="{{$cart->product->id}}">
                        <!-- <input type="hidden" name="product_name[]" value="{{$cart->product_name}}"> -->
                        <input type="hidden" name="quantity[]" value="{{$cart->product_qty}}">
                        <input type="hidden" name="price[]"
                          value="{{$cart->product->price}}" class="form-control price" >

                        <input type="hidden" name="discount[]"
                         class="form-control discount" >

                        <input type="hidden" name="total_amount[]" 
                          value="{{$cart->product_qty * $cart->product->price}}"
                           class="form-control total_amount" >
                @endforeach


      <div class="card-body">
        <div class="btn-group">
          <button type="button"
            onclick="PrintReceiptContent('print')"
            class="btn btn-dark"> <i class="fa fa-print"></i>Print
            </button>
            <button type="button"
            onclick="PrintReceiptContent('print')"
            class="btn btn-primary"> <i class="fa fa-print"></i>History
            </button>
            <button type="button"
            onclick="PrintReceiptContent('print')"
            class="btn btn-danger"> <i class="fa fa-print"></i>Report
            </button>

        </div>
          <div class="panel">
            <div class="row">                          

              <table class="table table-striped">
                <tr>
                  <td>
                  <label for="">Customer Name</label>
                  <input type="text" name="customer_name" id="" class="form-control">
                  </td>
                  <td>

                  <label for="">Customer Phone</label>
                  <input type="number" name="customer_phone" id="" class="form-control">
                  </td>
                </tr>
              </table>

              <td>Payment Method <br>
              <div class="">
              <span class="radio-item">
                <input type="radio" name="payment_method" id="payment_method"
                  class="true" value="cash" checked="checked">
                  <label for="payment_method"><i class="fa fa-money-bill text-success"></i>Cash</label>
              </span>
              <span class="radio-item">
                <input type="radio" name="payment_method" id="payment_method"
                  class="true" value="bank transfer">
                  <label for="payment_method"><i class="fa fa-university text-danger"></i>Bank Transfer</label>
              </span>
              <span class="radio-item">
                <input type="radio" name="payment_method" id="payment_method"
                  class="true" value="credit Card">
                  <label for="payment_method"><i class="fa fa-credit-card text-info"></i>Credit Card</label>
              </span>
              </td><br>

              <td> Payment
                 <input type="number" wire:model="pay_money" name="paid_amount"
                id="paid_amount" class="form-control">
              </td>

              <td> Returning Change 
                <input type="number" wire:model="balance" name="balance"
                id="balance" readonly class="form-control">
              </td>
              <td>
                <button class="btn-primary btn-lg btn-block mt-3">Save</button>
              </td>
              <td>
                <button class="btn-danger btn-lg btn-block mt-2">Calculator</button>
              </td>

              <div class="text-center">
                <a href="#" class="text-danger"><i class=" fa fa-sign-out-alt"></i></a>

                              </div>
                              </form>
                            </div>
                        </div>
                      </div>  
                    </div>
                </div>
              </div>  
          </form>                   
        </div>
    </div> 
</div> 




<!-- Order Details display -->


<div class="container-fluid">
    <div class="col-lg-12">
        <div class="row">
            <div class="col-md-9">
            <div class="card">
                    <div class="card-header"><h4 style="float:left">Recent Orders</h4>
              </div>

                        <div class="card-body">
              
                <Table class="table table-bordered table-left">
                    <thead>
                    <tr>
                    <th>id</th>
                    <th>Order Id</th>
                    <th>Product Name</th>
                    <th>Quantity</th>
                    <th>Price</th>
                    <th>Total Amount</th>
                    <th>Discount</th>
                    </tr>
                    </thead>

                    
                    @foreach($order_details as $order_detail)
                      <tr>
                     
                      <td>{{$order_detail->product_id}}</td>
                      <td></td>
                      <td></td>
                      <td></td>
                      <td></td>
                      <td>{{$order_detail->unitprice}}</td>
                      <td>{{$order_detail->amount}}</td>
                      <td></td>
                      </tr>
                      @endforeach

                   

                    <tbody>
                    </Table> 


                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

Advertisement

Answer

What I noticed in your code is that you might be expecting an answer in another place while your code is running somewhere else. First of all, you are writing a show(Order $order) function that collects parameters, while you are outputting it in the index page, so where you are calling the show function will expect a parameter, while the index will not expect a parameter

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement