首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular和Laravel -显示数据库中的最后一条记录得到了错误的结果

Angular和Laravel -显示数据库中的最后一条记录得到了错误的结果
EN

Stack Overflow用户
提问于 2019-11-07 16:48:06
回答 1查看 34关注 0票数 0

我正在开发一个Web门户,使用Angular-7作为前端,Laravel-5.8作为后端。我想要显示发布到数据库中的最后一个数据:

拉威尔

ApiController.php

代码语言:javascript
复制
public function showClient(Request $request)
{
    try{
         $client = Client::orderBy('id', 'desc')->first();   
          return response()->json($client, 200);
        }
        catch(QueryException $e)
        {
            $errorCode = $e->errorInfo[1];
            return response()->json($errorCode);
        }
} 

api.php

代码语言:javascript
复制
Route::get('showClient', 'ApiController@showClient');

角度

client.component.ts

代码语言:javascript
复制
export class ClientComponent implements OnInit {

  clientdetail = null;

  headers = {     //Token for API Authorization
    'Authorization' : this.token.get(),
    'X-Requested-With' : 'XMLHttpRequest'
  }


  constructor(
    private api: ApiService,
    private token: TokenService,
    private http: HttpClient,
    private router: Router,
    private notify: SnotifyService,
  ) { }

  ngOnInit() {
    window.dispatchEvent(new Event('load'));
    window.dispatchEvent(new Event('resize'));

    this.api.get('showClient', this.headers).subscribe(
      data => {console.log(data), this.clientdetail = data; this.dataHandlerclientdetail(data)}
    );
  }

  dataHandlerclientdetail(data){
    console.log(data.data);
  }

}

当我加载页面时,我假设要将最后的数据发布到表中,但表中的所有内容都显示出来了。

请指导我如何解决这个问题?谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-11-07 17:19:18

您可以使用last()方法

代码语言:javascript
复制
public function showClient(Request $request)
{
    try{
         $client = Client::select('*')->get()->last();   
          return response()->json($client, 200);
        }
        catch(QueryException $e)
        {
            $errorCode = $e->errorInfo[1];
            return response()->json($errorCode);
        }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58744969

复制
相关文章

相似问题

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