使用 AJAX Laravel 在获取请求中使用分页时未定义值

发布于 2025-01-16 16:52:39 字数 2165 浏览 0 评论 0原文

我正在使用 AJAX 进行获取请求。在我的 Laravel 控制器中,我尝试使用“->paginate(5);” 我得到了未定义的值,但是当我使用“->get();”时它工作得很好,但我想使用分页在我的表格中进行分页。我认为我如何输出值存在问题。 根据控制台中的检查,获取返回一个对象并分页返回一个数组。 我正在使用这个“$.each(response.data, function (key, item)”来输出值。请帮忙。

这是我的控制器

public function getOrders($store_id)
{
    $data = DB::table('stores')
    ->where('stores.id',$store_id)
    ->paginate(5);

    return response()->json(['data' => $data]);

}

这是我的 AJAX

function fetchOrder(){
    $.ajax({
            type: "GET",
            url: "/get-orders/"+store_id,
            dataType: "json",
            success: function (response) {

                if (response.data != null) {
                    console.log(response);
                    $('.OrderTbody').html("");
                    $.each(response.data, function (key, item) {
                        $('.OrderTbody').append('<tr><td>' + item.store_name + '</td></tr>');

                    });


                }
            }
        });
}

这是使用 paginate

在此处输入图像描述

这是使用正在工作的 get 的控制台中的响应

Object
data:
current_page: 1
data: [{…}]
first_page_url: "http://127.0.0.1:8000/get-orders/2?page=1"
from: 1
last_page: 1
last_page_url: "http://127.0.0.1:8000/get-orders/2?page=1"
links: (3) [{…}, {…}, {…}]
next_page_url: null
path: "http://127.0.0.1:8000/get-orders/2"
per_page: 15
prev_page_url: null
to: 1
total: 1
[[Prototype]]: Object
[[Prototype]]: Object

,这是使用不工作的 paginate 的控制台中的响应

{data: Array(1)}
data: Array(1)
0:
address: "Cubao, Quezon City, Metro Manila, Philippines"
approval: 1
close_time: "20:24:00"
created_at: "2022-03-21 12:22:18"
id: 2
lat: "14.6177663"
lng: "121.0571541"
open_time: "20:23:00"
profile_image: "support-1store-prof_image1647868469.jpg"
status: 1
store_name: "Kainan"
store_users_id: 3
updated_at: "2022-03-21 13:14:29"
zipcode: "3112"
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)
[[Prototype]]: Object

I'm doing get request using AJAX. In my Laravel Controller, I tried to use "->paginate(5);"
and I got undefined values but when I'm using "->get();" it works perfectly but I wanted to use paginate to have pagination in my table. I think there is an issue on how I output the values.
Get return an object and paginate return an array as per checking in console.
I'm using this "$.each(response.data, function (key, item)" to output the values. Please help.

This is my controller

public function getOrders($store_id)
{
    $data = DB::table('stores')
    ->where('stores.id',$store_id)
    ->paginate(5);

    return response()->json(['data' => $data]);

}

This is my AJAX

function fetchOrder(){
    $.ajax({
            type: "GET",
            url: "/get-orders/"+store_id,
            dataType: "json",
            success: function (response) {

                if (response.data != null) {
                    console.log(response);
                    $('.OrderTbody').html("");
                    $.each(response.data, function (key, item) {
                        $('.OrderTbody').append('<tr><td>' + item.store_name + '</td></tr>');

                    });


                }
            }
        });
}

This is example output using paginate

enter image description here

This is the response in console using the get which is working

Object
data:
current_page: 1
data: [{…}]
first_page_url: "http://127.0.0.1:8000/get-orders/2?page=1"
from: 1
last_page: 1
last_page_url: "http://127.0.0.1:8000/get-orders/2?page=1"
links: (3) [{…}, {…}, {…}]
next_page_url: null
path: "http://127.0.0.1:8000/get-orders/2"
per_page: 15
prev_page_url: null
to: 1
total: 1
[[Prototype]]: Object
[[Prototype]]: Object

and this is response in console using paginate that is NOT WORKING

{data: Array(1)}
data: Array(1)
0:
address: "Cubao, Quezon City, Metro Manila, Philippines"
approval: 1
close_time: "20:24:00"
created_at: "2022-03-21 12:22:18"
id: 2
lat: "14.6177663"
lng: "121.0571541"
open_time: "20:23:00"
profile_image: "support-1store-prof_image1647868469.jpg"
status: 1
store_name: "Kainan"
store_users_id: 3
updated_at: "2022-03-21 13:14:29"
zipcode: "3112"
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)
[[Prototype]]: Object

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜巴黎 2025-01-23 16:52:39

使用

$.each(response.data.data, function (key, item) {
    $('.OrderTbody').append('<tr><td>' + item.store_name + '</td></tr>');
});

Use

$.each(response.data.data, function (key, item) {
    $('.OrderTbody').append('<tr><td>' + item.store_name + '</td></tr>');
});
柳若烟 2025-01-23 16:52:39

尝试像这样导入数据:

   $data = DB::table('stores')
    ->where('stores.id',$store_id)
    ->paginate(5)
    ->toArray();

添加 ->toArray() 将简化您的对象,因此 JSON 序列化对于 Laravel 来说将更加“简单”。

try importing your data like this :

   $data = DB::table('stores')
    ->where('stores.id',$store_id)
    ->paginate(5)
    ->toArray();

adding ->toArray() will simplify your objects so the JSON serialization will be more "easy" for the Laravel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文