Laravel: @foreach 中的未定义变量
Laravel: Undefined variable in @foreach
我想在网络上显示 order_details table,但是 @foreach 循环显示 未定义的变量 $order_details
这是我的 @foreach 循环
@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
我的订单控制器包含这个:
public function index()
{
$order_details=Order_Detail::all();
return view('orders.index',['order_details' => $order_details]);
}
index.blade.php 仅包含 css 和 javascript 代码。
我的 index.blade.php 像这样
进一步连接到 livewire(order.blade.php)
@livewire('order')
我的 livewire(order.blade.php) 包含此代码
<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>
我在您的代码中注意到,您可能在其他地方期待答案,而您的代码在其他地方 运行。
首先,你在写一个收集参数的show(Order $order)函数,而你是在index页面输出的,所以你调用show函数的地方会期待一个参数,而index不会期待一个参数
我想在网络上显示 order_details table,但是 @foreach 循环显示 未定义的变量 $order_details 这是我的 @foreach 循环
@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
我的订单控制器包含这个:
public function index()
{
$order_details=Order_Detail::all();
return view('orders.index',['order_details' => $order_details]);
}
index.blade.php 仅包含 css 和 javascript 代码。 我的 index.blade.php 像这样
进一步连接到 livewire(order.blade.php)@livewire('order')
我的 livewire(order.blade.php) 包含此代码
<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>
我在您的代码中注意到,您可能在其他地方期待答案,而您的代码在其他地方 运行。 首先,你在写一个收集参数的show(Order $order)函数,而你是在index页面输出的,所以你调用show函数的地方会期待一个参数,而index不会期待一个参数