调用未定义的方法 stdClass::links()



>我正在向 Web API 发送请求,该 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
}
} 

波纹管是控制器代码的一部分:

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]);

不知道您是否仍然遇到问题,但您正在使用

$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

如果您希望json响应中提供->links(),则需要在转换和转换之前附加它,或设置为新变量:

$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是什么以及为什么函数不起作用。

最新更新