尝试使用数组仅获取 Laravel 中每个订单的特定产品
Posted
技术标签:
【中文标题】尝试使用数组仅获取 Laravel 中每个订单的特定产品【英文标题】:Trying to get only the specific products for each order in Laravel using arrays 【发布时间】:2021-05-22 05:41:13 【问题描述】:我正在尝试基于两个表为用户显示订单页面:orders 和 order_product。
orders 表保存订单的所有有用数据,如模型中所示:
Order.php
protected $fillable = [
'user_id', 'billing_fname', 'billing_lname', 'billing_email', 'billing_phone','billing_address',
'billing_county', 'billing_locality', 'billing_zipcode', 'billing_total', 'shipped'
];
public function user()
return $this->belongsTo('App\User');
public function products()
return $this->belongsToMany('App\Product')->withPivot('quantity');
order_product 表显示有关该订单购买了多少产品以及每种产品的数量的信息。
OrderProduct.php
protected $table = 'order_product';
protected $fillable = ['order_id', 'product_id', 'quantity'];
现在,问题是我想在视图中显示所有这些有界信息,但我从所有订单中获取所有产品 ID,而不仅仅是特定订单:
myorders.blade.php
<ul class="order-list">
@foreach($orders as $order)
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">Order $order->id</h5>
<small>
@if(!$o->shipped)
Not shipped.
@else
Shipped.
@endif
</small>
</div>
<p class="mb-1">User: $order->billing_fname $order->billing_lname | Date: $order->created_at | Total: $order->billing_total $</p>
<small>Adresa: $order->billing_county $order->billing_address $order->billing_city</small>
</a>
@foreach($products as $product)
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">$product->name</h5>
<small class="text-muted">Cantitate: $product->quantity</small>
</div>
<p class="mb-1">Price: $product->price</p>
</a>
@endforeach
</div>
@endforeach
</ul>
在控制器中,我尝试访问 3 个不同的 foreach 语句中的所有数据,如下所示:
订单控制器
public function index()
$user_id = Auth::user()->id;
$orders = Order::where('user_id', $user_id)->get(); //from orders table I need all the orders placed by the authenticate user
$items = array();
foreach($orders as $order)
$items[] = OrderProduct::where('order_id', $order->id)->first(); //from order_product (the pivot table) I need all the data from order_id equals the id in orders
$products = array();
foreach($items as $item)
$prod = Product::where('id', $item->product_id)->first(); //searching for the products in the products table with the id's from order_product
$products[] = $prod;
return view('orders.myorders', array(
'orders' => $orders,
'products' => $products
));
// dd($products);
我得到的是每个订单的所有产品,所以视图看起来像:
一阶编号:
-数组中找到的所有产品
二阶编号:
-再次在数组中找到所有产品
-等等,而我只需要该订单的特定产品
如果我不够清楚,请随时提出任何问题。我是这个领域的新手,如果有任何建议,我将不胜感激。
非常感谢。
【问题讨论】:
【参考方案1】:您可以通过以下更改以更少的代码完成此操作:
Order.php 的变化
public function orderProducts()
return $this->hasMany('App\OrderProduct');
OrderProduct.php 中的更改
public function products()
return $this->hasOne('App\Product')->withPivot('quantity');
OrderController.php 中的更改:
public function index()
$user_id = Auth::user()->id;
$orders = Order::where('user_id', $user_id)->get();
return view('orders.myorders', array('orders' => $orders));
以及 myorders.blade.php 的变化:
<ul class="order-list">
@foreach($orders as $order)
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">Order $order->id</h5>
<small>
@if(!$o->shipped)
Not shipped.
@else
Shipped.
@endif
</small>
</div>
<p class="mb-1">User: $order->billing_fname $order->billing_lname | Date: $order->created_at | Total: $order->billing_total $</p>
<small>Adresa: $order->billing_county $order->billing_address $order->billing_city</small>
</a>
@foreach($order->orderProducts()->get() as $orderProduct)
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">$orderProduct->product->name</h5>
<small class="text-muted">Cantitate: $orderProduct->product->quantity</small>
</div>
<p class="mb-1">Price: $orderProduct->product->price</p>
</a>
@endforeach
</div>
@endforeach
</ul>
【讨论】:
第二个foreach有问题,它试图访问非对象的属性。我尝试将其替换为 $orderProduct->products()->name 以访问 products() 函数,但发生以下情况:调用未定义的方法 Illuminate\Database\Eloquent\Relations\HasMany::withPivot()。我认为 withPivot() 仅适用于 belongsToMany 关系【参考方案2】:我通过以下逻辑解决了这个问题:
//details of a specific order
public function index($id)
$orders = Order::where('_id', $id)->first();
$details = OrderProduct::where('order_id', $id)->get(); //for an order with the same id we can have multiple products => array
$product_id = OrderProduct::where('order_id', $id)->get(['product_id']); //accessing the product from the linked table
$items = array();
$products = array();
foreach($details as $detail)
$items[] = $detail->product_id; //an array with all the details from the specific order
$prod = Product::findOrFail($detail->product_id); //we add the products with the corresponding id in our products array
$products[] = $prod;
return view('orders.myorder', array(
'orders' => $orders,
'details' => $details,
'products' => $products //returning all the details, orders for accessing the details from Orders, details for accessing all the product's id from that order (OrderProduct and products for getting access to all the products (Products)
));
// dd($items);
以及显示所有详细信息的刀片文件:
<div class="container">
<h3 class="text-center"> __('Order no.') $orders['id']</h3>
<div class="container-fluid">
<div class="row">
<div class="col-sm-4">
<h5 class="my-5"> __('User Details') </h5>
<div class="form-group">
<p><strong> __('First Name') : </strong><em><strong>$orders['billing_fname'] $orders['billing_lname']</strong></em></p>
</div>
<div class="form-group">
<p><strong> __('Last Name') : </strong><em><strong>$orders['billing_email']</strong></em></p>
</div>
<div class="form-group">
<p><strong> __('Phone') : </strong><em><strong> $orders['billing_phone']</strong></em></p>
</div>
<div class="form-group">
<p><strong> __('Address') : </strong><em><strong> $orders['billing_address']</strong></em></p>
</div>
<div class="form-group">
<p><strong> __('County') : </strong><em><strong> $orders['billing_county']</strong></em></p>
</div>
<div class="form-group">
<p><strong> __('City') : </strong><em><strong> $orders['billing_city']</strong></em></p>
</div>
<div class="form-group">
<p><strong> __('Zipcode') : </strong><em><strong> $orders['billing_zipcode']</strong></em></p>
</div>
<div class="form-group">
<p><strong> __('Total') : </strong><em><strong> $orders['billing_total']</strong></em></p>
</div>
<div class="form-group">
<p><strong>Status: </strong><em><strong> $orders['shipped'] === "1" ? __('Shipped') : __('Not shipped') </strong></em></p>
</div>
</div>
<div class="col-sm-8">
<h5 class="text-center mt-5"> __('Ordered products') </h5>
<table class="table mt-3">
<thead>
<tr>
<th> __('Product Name') </th>
<th> __('Image') </th>
<th> __('Price') </th>
<th> __('Quantity') </th>
<th> __('Date') </th>
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>
<a href="url(app()->getLocale().'/details', $product->slug)" style="text-decoration:none;"><p> $product->name </p></a>
</td>
<td><a href="url(app()->getLocale().'/details', $product->slug)"><img src="../../img/$product['image']" ></a></td>
<td>
<p>$product->price RON</p>
</td>
@foreach($details as $detail)
@if($product->id == $detail->product_id)
<td><p> $detail->quantity buc</p></td>
@endif
@endforeach
<td>
<p>$orders['created_at']</p>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="float-right m-4">
<a class="btn btn-info m-4" href=" url(app()->getLocale().'/myorders') "> __('Back') </a>
</div>
</div>
</div>
</div>
</div>
这可能有点难以理解,我的逻辑很复杂,但我希望你们中的一些人能从中得到一些帮助。
【讨论】:
以上是关于尝试使用数组仅获取 Laravel 中每个订单的特定产品的主要内容,如果未能解决你的问题,请参考以下文章
使用laravel Carbon,如何通过数字获取翻译后的名字?
如何通过 Laravel 中的 API 从 Shopify 商店获取所有订单