Php 如何在laravel中获得单个订单的项目?
我还是拉威尔的初学者。我有三种型号:商品、商品、订单、订单。 我想在show function中获取每个产品的项目 桌子 订单控制器 项目内模型 有序模型Php 如何在laravel中获得单个订单的项目?,php,laravel,Php,Laravel,我还是拉威尔的初学者。我有三种型号:商品、商品、订单、订单。 我想在show function中获取每个产品的项目 桌子 订单控制器 项目内模型 有序模型 如果希望在1页上显示如下内容: items item_order order ------- -------- -------- id order_id id name item_id name 您可以在控制器中执行以下操作: public function show
如果希望在1页上显示如下内容:
items item_order order
------- -------- --------
id order_id id
name item_id name
您可以在控制器中执行以下操作:
public function show($id)
{
$account = AccountModel::find($id);
if (!is_null($account)) {
$items = Item::paginate(10);
$order = Order::paginate(10);
$item_order= Item_Order::paginate(10);
return view('vendor.multiauth.admin.orders.show', [
'account' => $account,
'items' => $items,
'order' => $order,
'item_order' => $item_order,
]);
}
return redirect()->back();
}
然后在您的模型中执行以下操作:
return $this->hasOne(ModelOfThePageYouShow::class, 'account_id');
在你看来:
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<tr>
<th>id</th>
<th>item</th>
<th>account_id</th>
</tr>
@foreach ($items as $item)
<tr>
<th>{{ $item->id }}</th>
<th>{{ $item->name}}</th>
<th>{{ $item->account_id }}</th>
</tr>
@endforeach
@foreach ($item_order as $item_order)
<tr>
<th>{{ $item_order->id }}</th>
<th>{{ $item_order->name}}</th>
<th>{{ $item_order->account_id }}</th>
</tr>
@endforeach
@foreach ($order as $order)
<tr>
<th>{{ $order->id }}</th>
<th>{{ $order->item}}</th>
<th>{{ $order->account_id }}</th>
</tr>
@endforeach
</table>
当然,你必须改变你所拥有的东西的价值,但我希望你能用这个来解决它
我可能在某个地方犯了一些错误,如果有人想纠正我,请这样做。您需要每个项目的订单,对吗?项目\u订单这里的下划线看起来有点像每个产品的项目?不能理解实际的问题,你想做什么?如果您想要订单的所有项目,$order->items将完成此工作。
items item_order order
------- -------- --------
id order_id id
name item_id name
public function show($id)
{
$account = AccountModel::find($id);
if (!is_null($account)) {
$items = Item::paginate(10);
$order = Order::paginate(10);
$item_order= Item_Order::paginate(10);
return view('vendor.multiauth.admin.orders.show', [
'account' => $account,
'items' => $items,
'order' => $order,
'item_order' => $item_order,
]);
}
return redirect()->back();
}
return $this->hasOne(ModelOfThePageYouShow::class, 'account_id');
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<tr>
<th>id</th>
<th>item</th>
<th>account_id</th>
</tr>
@foreach ($items as $item)
<tr>
<th>{{ $item->id }}</th>
<th>{{ $item->name}}</th>
<th>{{ $item->account_id }}</th>
</tr>
@endforeach
@foreach ($item_order as $item_order)
<tr>
<th>{{ $item_order->id }}</th>
<th>{{ $item_order->name}}</th>
<th>{{ $item_order->account_id }}</th>
</tr>
@endforeach
@foreach ($order as $order)
<tr>
<th>{{ $order->id }}</th>
<th>{{ $order->item}}</th>
<th>{{ $order->account_id }}</th>
</tr>
@endforeach
</table>