使用 AJAX Laravel 在获取请求中使用分页时未定义值
我正在使用 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Use
尝试像这样导入数据:
添加
->toArray()
将简化您的对象,因此 JSON 序列化对于 Laravel 来说将更加“简单”。try importing your data like this :
adding
->toArray()
will simplify your objects so the JSON serialization will be more "easy" for the Laravel.