b4m-oss / capetown-cms

Capetown CMS: Navigating a New Era of Content Management. Embark on a new journey in content management, turning the Cape of Good Hope toward a future of limitless possibilities.
MIT License
3 stars 0 forks source link

CRUDのベースとなるTraitの作成 #4

Open kohki-shikata opened 2 weeks ago

kohki-shikata commented 2 weeks ago

前に作ったやつがあるので、それを流用 https://gist.github.com/kohki-shikata/5061e22523eb43a472a0834179326cf7


改善項目

kohki-shikata commented 1 week ago

よくよく考えたら、index()メソッドが即座に全件返すのも問題っちゃ問題だな

kohki-shikata commented 1 week ago

編集中ロック機能のTrait

https://gist.github.com/kohki-shikata/6b89309d8b326fc4763d7d5e47ab6415

kohki-shikata commented 1 week ago

よくよく考えたら、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()];
        }
    }