【发布时间】:2019-10-17 07:38:44
【问题描述】:
我正在制作一个具有特定当前步骤的订单表,在这种情况下,该步骤是“NESTEN”和一些其他 where 语句来获取我需要的订单。
因为我不想在一页上包含超过 400 个订单的列表,所以我想使用 laravel 分页功能,该功能还可以提高超过 100.000 条记录的列表的性能。 laravel 分页按原样工作,但是当我想使用我制作的过滤器时问题就来了。
我制作了一个下拉列表,需要根据列表中订单的材料进行过滤。在表格中,我看到了过滤后的订单,但分页的页数和订单数与以前相同。因此,问题在于从查询中过滤集合后分页没有更新。
我已经尝试了一些谷歌搜索,发现了几个没有解决我的问题甚至导致更多问题的解决方案......
还增加了$orders->过滤功能,将不符合过滤条件但没有结果的订单剔除掉……
为了让它暂时易于理解,我在 Route 文件中添加了代码。
我的路线如下
Route::get('orders/nesten', function(Request $request) {
$orders = ShopOrder::where([
['ItemCode', 'LIKE', 'CM%'],
['Exact', '=', null],
['Nesting', '=', null],
['IsOnHold', '!=', 1],
['ShopOrderRoutingStepPlanCount', '!=', 0]
])->paginate(50);
$filteredCollection = $orders->filter(function ($order) use($request) {
if($request->exists('material')) {
return $order->getCurrentStep() == 'Nesten'
&& $order->getMaterial() == $request->get('material');
}
return $order->getCurrentStep() == 'Nesten';
});
$orders->setCollection($filteredCollection);
return view('dashboard/actions/Nesten')->with('shopOrders', $orders);
});
在 ShopOrder 模型中,我将函数 ->getMaterial() 和 ->getCurrentStep() 声明为
public function routingStepPlans() {
return $this->hasMany('App\Models\Exact\ShopOrder\RoutingStepPlan', 'ShopOrder', 'ID');
}
public function materialPlans() {
return $this->hasMany('App\Models\Exact\ShopOrder\MaterialPlan', 'ShopOrder', 'ID');
}
public function getCurrentStep() {
$current = $this->routingStepPlans()->where('LineNumber', ($this->timeTransactions()->count() + 1))->first();
if(isset($current->Description)) {
return $current->Description;
}
return 'Afgerond';
}
public function getMaterial() {
$material = $this->materialPlans()->where('ItemCode', 'LIKE', 'TAP%')->first();
if(isset($material->Description)) {
return $material->Description;
}
return '-';
}
最后是观点
<table class="table table-striped table-bordered no-width">
<thead>
<tr>
<th>Order nummer</th>
<th>SKU</th>
<th>Omschrijving</th>
<th>Aantal</th>
<th>Datum</th>
<th>Deadline</th>
<th>Materiaal</th>
<th>DXF?</th>
</tr>
</thead>
<tbody>
@foreach($shopOrders as $shopOrder)
<tr>
<td>{{ $shopOrder->ShopOrderNumber }}</td>
<td>{{ $shopOrder->ItemCode }}</td>
<td>{{ str_replace('Car Mats', '',$shopOrder->Description) }}</td>
<td>{{ $shopOrder->PlannedQuantity }}</td>
<td>{{ $shopOrder->PlannedStartDate }} </td>
<td>{{ $shopOrder->PlannedDate }}</td>
<td>{{ $shopOrder->getMaterial() }}</td>
<td>{{ $shopOrder->hasDxf() }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $shopOrders->total() }}
{{ $shopOrders->appends(['material' => Request::get('material')])->render() }}
我希望分页中的 1 页以 orders/nesten?material=Saxony%20Zwart&page=1 作为 url,因为该材料有 9 个订单。
但目前还是有151页,跟去orders/nesten一样。
【问题讨论】:
标签: php laravel eloquent laravel-6