首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用未定义方法stdClass::link()

调用未定义方法stdClass::link()
EN

Stack Overflow用户
提问于 2018-10-11 10:56:36
回答 1查看 2.3K关注 0票数 1

我正在发送一个请求到一个web API,它用一个编码的分页响应来响应,我正在接收这个响应并成功地解码它。答复如下:

代码语言:javascript
复制
{#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是控制器代码的一部分:

代码语言:javascript
复制
  if ($response->code=='1')
            {
//                dd($response->data);
                $data=$response->data;

//
//                dd($data);
                 return view('posts',compact('data'));
            }

以下是视图代码:

代码语言:javascript
复制
 <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代码是:

代码语言:javascript
复制
$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]);
EN

回答 1

Stack Overflow用户

发布于 2018-10-16 17:52:59

不知道你还有没有问题,但你在用

代码语言:javascript
复制
$post = post::orderBy('created_at','asc')->paginate(10);

紧跟其后

代码语言:javascript
复制
$post = $post->toArray();

最后

代码语言:javascript
复制
return response()>json([
  "message" => "success",
  "code" => "1",
  "data" => $post
]);

有一种方法->links()->paginate()的结果中是可用的,但在使用->toArray()时是不可用的,当然也不能通过response()->json()将其转换为json

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

代码语言:javascript
复制
$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是什么,以及为什么函数不能在其上工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52758335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档