如何使我的控制器返回的數據是一個json不重複我的代碼。Laravel如何使所有控制器返回數據爲json
樣品控制器
public function getTeams(Request $request){
$result = Team::where('competitionId',$request->input('competitionId',9224))
->orderBy('teamName')
->get(['teamId as id','teamName as name']);
return response($result, 200)
->header('Access-Control-Allow-Origin', '*')
->header('Content-Type', 'application/json');
}
public function getTeamStats(Request $request) {
if($request->id){
$result = TeamCompetitionStatistics::getTeamStats($request->id);
return response($result, 200)
->header('Access-Control-Allow-Origin', '*')
->header('Content-Type', 'application/json');
}
}
,你可以看到我已經重複這一節兩次
return response($result, 200)
->header('Access-Control-Allow-Origin', '*')
->header('Content-Type', 'application/json');
是他們的一種方式,以更好的方式做到這一點?