Laravel 9 Inertia 分页

huangapple go评论69阅读模式
英文:

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()-&gt;get(&#39;id&#39;, 0);
    $order = null;
    $order = OrderItem::with([&#39;items&#39;])-&gt;find($orderId);

    if ($orderId != 0) {
        if ($order == null) {
            return Inertia::render(&#39;IDashboard/utils/IError&#39;);
        }
    }

    return Inertia::render(
        &#39;Orders/IData&#39;,
        [&#39;order&#39; =&gt; $order]
    );
}

I have tried with:

public function order()
{
    $orderId = request()-&gt;get(&#39;id&#39;, 0);
    $order = null;
    $order = OrderItem::find($orderId);

    if ($orderId != 0) {
        if ($order == null) {
            return Inertia::render(&#39;IDashboard/utils/IError&#39;);
        }
    }

    $items = Item::where(&#39;order_item_id&#39;, $orderId)-&gt;paginate(10);

    return Inertia::render(
        &#39;Orders/IData&#39;,
        [&#39;order&#39; =&gt; $order, &#39;itemsList&#39; =&gt; $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,
},
&lt;IItemCalculate
   v-for=&quot;(
       item, index
       ) in orderItems&quot;
       :order=&quot;order&quot;
       :item=&quot;item&quot;
       :key=&quot;index&quot;/&gt;
&lt;IPagination :links=&quot;itemsList.links&quot; /&gt; //general pagination component:

the path i am using is:

Route::get(&#39;/order&#39;, &#39;App\Http\Controllers\ItemController@order&#39;)-&gt;name(&#39;order&#39;);

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-&gt;items()-&gt;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(&#39;order/{id}&#39;, [...])-&gt;name(&#39;orders&#39;);

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()-&gt;find($id);
    
    if(is_null($order)) {
        return Inertia::render(&#39;IDashboard/utils/IError&#39;);
    }

    $items = Item::query()
                -&gt;where(&#39;order_item_id&#39;, $order-&gt;id)
                -&gt;paginate(10);

    return Inertia::render(&#39;Orders/IData&#39;, compact(&#39;order&#39;,&#39;items&#39;));

}

If you really want to include the order_id as a query string you can look into this. I didn't try it yet.

Appending Query String Values in Pagination

huangapple
  • 本文由 发表于 2023年3月1日 10:14:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定