我正在开发一个Web门户,使用Angular-7作为前端,Laravel-5.8作为后端。我想要显示发布到数据库中的最后一个数据:
拉威尔
ApiController.php
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
Route::get('showClient', 'ApiController@showClient');角度
client.component.ts
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);
}
}当我加载页面时,我假设要将最后的数据发布到表中,但表中的所有内容都显示出来了。
请指导我如何解决这个问题?谢谢
发布于 2019-11-07 17:19:18
您可以使用last()方法
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);
}
}https://stackoverflow.com/questions/58744969
复制相似问题