Open kohki-shikata opened 2 weeks ago
よくよく考えたら、index()メソッドが即座に全件返すのも問題っちゃ問題だな
よくよく考えたら、index()メソッドが即座に全件返すのも問題っちゃ問題だな
改善した
/**
* 一覧表示
*/
public function index()
{
try {
$model = $this->getModel();
// 最大件数を.envから取得
$maxResults = (int) env('MAX_RESULTS', 0); // デフォルト値を0に設定
// 最大件数が設定されている場合、制限をかける
if ($maxResults > 0) {
$records = $model::limit($maxResults)->get();
} else {
$records = $model::all(); // 最大件数が設定されていない場合は全件取得
}
return $records; // JSONではなく直接返す
} catch (\Exception $e) {
return ['message' => 'Error fetching records', 'error' => $e->getMessage()];
}
}
前に作ったやつがあるので、それを流用 https://gist.github.com/kohki-shikata/5061e22523eb43a472a0834179326cf7
改善項目