英文:
Laravel 9 Inertia pagination
问题
I have the order function, in which it shows the items related to said order, what I want to do is, paginate only the items within the order.
public function order()
{
$orderId = request()->get('id', 0);
$order = null;
$order = OrderItem::with(['items'])->find($orderId);
if ($orderId != 0) {
if ($order == null) {
return Inertia::render('IDashboard/utils/IError');
}
}
return Inertia::render(
'Orders/IData',
['order' => $order]
);
}
I have tried with:
public function order()
{
$orderId = request()->get('id', 0);
$order = null;
$order = OrderItem::find($orderId);
if ($orderId != 0) {
if ($order == null) {
return Inertia::render('IDashboard/utils/IError');
}
}
$items = Item::where('order_item_id', $orderId)->paginate(10);
return Inertia::render(
'Orders/IData',
['order' => $order, 'itemsList' => $items]
);
}
But the problem in this other case is that, when doing the pagination in the Vue component, and hitting page 2, it does not show me any information. Child component.
props: {
is: Function,
order: Object,
orderItems: Object,
itemsList: Object,
},
<IItemCalculate
v-for="(item, index) in orderItems"
:order="order"
:item="item"
:key="index"/>
<IPagination :links="itemsList.links" /> //general pagination component:
The path I am using is:
Route::get('/order', 'App\Http\Controllers\ItemController@order')->name('order');
Errors.
The error is that, at the moment of giving page 2, or next, the content is not displayed, or when I do the pagination within the relationship, that is $items = $order->items()->paginate(10)
, it throws the error "member function items() on null," also when going to the next page.
Thank you very much, I'm up to date.
英文:
I have the order function, in which it shows the items related to said order, what I want to do is, paginate only the items within the order.
public function order()
{
$orderId = request()->get('id', 0);
$order = null;
$order = OrderItem::with(['items'])->find($orderId);
if ($orderId != 0) {
if ($order == null) {
return Inertia::render('IDashboard/utils/IError');
}
}
return Inertia::render(
'Orders/IData',
['order' => $order]
);
}
I have tried with:
public function order()
{
$orderId = request()->get('id', 0);
$order = null;
$order = OrderItem::find($orderId);
if ($orderId != 0) {
if ($order == null) {
return Inertia::render('IDashboard/utils/IError');
}
}
$items = Item::where('order_item_id', $orderId)->paginate(10);
return Inertia::render(
'Orders/IData',
['order' => $order, 'itemsList' => $items]
);
}
But the problem in this other case is that, when doing the pagination in the Vue component, and hitting page 2, it does not show me any information. Child component.
props: {
is: Function,
order: Object,
orderItems: Object,
itemsList: Object,
},
<IItemCalculate
v-for="(
item, index
) in orderItems"
:order="order"
:item="item"
:key="index"/>
<IPagination :links="itemsList.links" /> //general pagination component:
the path i am using is:
Route::get('/order', 'App\Http\Controllers\ItemController@order')->name('order');
Errores.
The error is that, at the moment of giving page 2, or next, the content is not displayed, or The error is that, at the moment of giving page 2, or next, the content is not displayed, or when I do the pagination within the relationship, that is $items = $order->items()->paginate(10)
, throws the error, member function items() on null, also when going to the next page.
Thank you very much, I'm up to date.
答案1
得分: 1
你可以通过将order_id
包含在路径中而不是查询中来解决此问题,例如:127.0.0.1:8000/order/1
,然后稍微修改控制器。
Route::get('order/{id}', [...])->name('orders');
由于您想在订单不存在时呈现不同的页面,您可以跳过“Route-Model Binding”。
public function order($id) {
$order = Order::query()->find($id);
if(is_null($order)) {
return Inertia::render('IDashboard/utils/IError');
}
$items = Item::query()
->where('order_item_id', $order->id)
->paginate(10);
return Inertia::render('Orders/IData', compact('order','items'));
}
如果您真的想将order_id
包含为查询字符串,可以查看这个。我还没有尝试过。
英文:
You can solve this by including the order_id
in the path instead of query like : 127.0.0.1:8000/order/1
then modify the controller a little.
Route::get('order/{id}', [...])->name('orders');
Since you want to render different page in case the order
do not exists you can skip the Route-Model Binding
.
public function order($id) {
$order = Order::query()->find($id);
if(is_null($order)) {
return Inertia::render('IDashboard/utils/IError');
}
$items = Item::query()
->where('order_item_id', $order->id)
->paginate(10);
return Inertia::render('Orders/IData', compact('order','items'));
}
If you really want to include the order_id
as a query string you can look into this. I didn't try it yet.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论