我正在发送一个请求到一个web API,它用一个编码的分页响应来响应,我正在接收这个响应并成功地解码它。答复如下:
{#225 ▼
+"message": "تم بنجاح"
+"code": "1"
+"data": {#220 ▼
+"current_page": 1
+"data": array:10 [▶]
+"from": 1
+"last_page": 2
+"next_page_url": "http://localhost:8000/api/getpostsadmin?page=2"
+"path": "http://localhost:8000/api/getpostsadmin"
+"per_page": 10
+"prev_page_url": -1
+"to": 10
+"total": 11
}
} Bellow是控制器代码的一部分:
if ($response->code=='1')
{
// dd($response->data);
$data=$response->data;
//
// dd($data);
return view('posts',compact('data'));
}以下是视图代码:
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Title AR</th>
<th>Title EN</th>
<th>Description AR</th>
<th>Description EN</th>
<th>Created At</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Title AR</th>
<th>Title EN</th>
<th>Description AR</th>
<th>Description EN</th>
<th>Created At</th>
</tr>
</tfoot>
<tbody>
@foreach($data->data as $datum)
<tr>
<td>{{$datum->id}}</td>
<td>{{$datum->title_ar}}</td>
<td>{{$datum->title_en}}</td>
<td>{{$datum->description_ar}}</td>
<td>{{$datum->description_en}}</td>
<td>{{$datum->created_at}}</td>
</tr>
@endforeach
</tbody>
</table>
{!! $data->links() !!}
</div>为什么这个错误总是出现?我尝试了render()方法和{!! $data->data->links() !!},但是都没有效果。
返回响应的API代码是:
$post=post::orderBy('created_at','asc')->paginate(10); $post=$post->toArray(); $post=public_functions::remove_nulls($post);
return response()>json(["message"=>"success","code"=>'1','data'=>$post]);发布于 2018-10-16 17:52:59
不知道你还有没有问题,但你在用
$post = post::orderBy('created_at','asc')->paginate(10);紧跟其后
$post = $post->toArray();最后
return response()>json([
"message" => "success",
"code" => "1",
"data" => $post
]);有一种方法->links()在->paginate()的结果中是可用的,但在使用->toArray()时是不可用的,当然也不能通过response()->json()将其转换为json。
如果希望->links()在您的json响应中可用,则需要在转换和转换之前附加它,或者将其设置为新变量:
$post = post::orderBy('created_at','asc')->paginate(10);
$links = $post->links();
$post = $post->toArray();
$post["links"] = $links;
return response()>json([
"message" => "success",
"code" => "1",
"data" => $post
]);在这种情况下,您应该能够在视图中调用{!! $data->links !!}并正确地呈现分页链接。只需了解$post是什么,以及为什么函数不能在其上工作。
https://stackoverflow.com/questions/52758335
复制相似问题