コードロード

エラー討伐

【Laravel】定数をクラスファイルで管理する

定数をクラスファイルで管理する

app/Consts/CommonConst.php を作成した。

<?php

namespace App\Consts;

class CommonConst {

  const MESSAGE = 'message';

  const RESULT = 'success';

}

config/app.phpエイリアスを追加

<?php

return = [
    // 省略

  'aliases' => [
    // 省略
    'CommonConst' => \App\Consts\CommonConst::class,
  ]
]

app/Http/Controllers/Api/UserController.php で使用する

<?php
    public function index()
    {
        $users = User::where('id', Auth::id())->get();
        return response()->json([CommonConst::MESSAGE => CommonConst::RESULT, 'user_info' => $users]);
    }

参考

qiita.com