LaravelのFilamentで苦戦したポイント

# エラーが起きるポイント

### config/filament.php を生成するコマンド
```
php artisan vendor:publish --tag=filament-config
```
## ファイルの設定
### config/filament.php
```
<?php

return [


    'broadcasting' => [

    ],



    'default_filesystem_disk' => env('FILAMENT_FILESYSTEM_DISK', 'public'),


    'assets_path' => null,

    'cache_path' => base_path('bootstrap/cache/filament'),

    'livewire_loading_delay' => 'default',


    'auth' => [
        'guard' => 'web', // Laravelのデフォルト認証ガード
        'middleware' => ['web'], // 認証とセッション管理のために 'web' ミドルウェアを適用
    ],
];
```

## Livewireを再インストール
```
composer require livewire/livewire
php artisan vendor:publish --tag=livewire
```
## FilamentとLivewireを更新
```
composer update filament/filament
php artisan migrate
```

### config/session.php のコード
```
<?php

use Illuminate\Support\Str;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Session Driver
    |--------------------------------------------------------------------------
    |
    | This option determines the default session driver that is utilized for
    | incoming requests. Laravel supports a variety of storage options to
    | persist session data. Database storage is a great default choice.
    |
    | Supported: "file", "cookie", "database", "apc",
    |            "memcached", "redis", "dynamodb", "array"
    |
    */

    'driver' => env('SESSION_DRIVER', 'database'),

    /*
    |--------------------------------------------------------------------------
    | Session Lifetime
    |--------------------------------------------------------------------------
    |
    | Here you may specify the number of minutes that you wish the session
    | to be allowed to remain idle before it expires. If you want them
    | to expire immediately when the browser is closed then you may
    | indicate that via the expire_on_close configuration option.
    |
    */

    'lifetime' => (int) env('SESSION_LIFETIME', 120),

    'expire_on_close' => false,


    /*
    |--------------------------------------------------------------------------
    | Session Encryption
    |--------------------------------------------------------------------------
    |
    | This option allows you to easily specify that all of your session data
    | should be encrypted before it's stored. All encryption is performed
    | automatically by Laravel and you may use the session like normal.
    |
    */

    'encrypt' => env('SESSION_ENCRYPT', false),

    /*
    |--------------------------------------------------------------------------
    | Session File Location
    |--------------------------------------------------------------------------
    |
    | When utilizing the "file" session driver, the session files are placed
    | on disk. The default storage location is defined here; however, you
    | are free to provide another location where they should be stored.
    |
    */

    'files' => storage_path('framework/sessions'),

    /*
    |--------------------------------------------------------------------------
    | Session Database Connection
    |--------------------------------------------------------------------------
    |
    | When using the "database" or "redis" session drivers, you may specify a
    | connection that should be used to manage these sessions. This should
    | correspond to a connection in your database configuration options.
    |
    */

    'connection' => env('SESSION_CONNECTION'),

    /*
    |--------------------------------------------------------------------------
    | Session Database Table
    |--------------------------------------------------------------------------
    |
    | When using the "database" session driver, you may specify the table to
    | be used to store sessions. Of course, a sensible default is defined
    | for you; however, you're welcome to change this to another table.
    |
    */

    'table' => env('SESSION_TABLE', 'sessions'),

    /*
    |--------------------------------------------------------------------------
    | Session Cache Store
    |--------------------------------------------------------------------------
    |
    | When using one of the framework's cache driven session backends, you may
    | define the cache store which should be used to store the session data
    | between requests. This must match one of your defined cache stores.
    |
    | Affects: "apc", "dynamodb", "memcached", "redis"
    |
    */

    'store' => env('SESSION_STORE'),

    /*
    |--------------------------------------------------------------------------
    | Session Sweeping Lottery
    |--------------------------------------------------------------------------
    |
    | Some session drivers must manually sweep their storage location to get
    | rid of old sessions from storage. Here are the chances that it will
    | happen on a given request. By default, the odds are 2 out of 100.
    |
    */

    'lottery' => [2, 100],

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Name
    |--------------------------------------------------------------------------
    |
    | Here you may change the name of the session cookie that is created by
    | the framework. Typically, you should not need to change this value
    | since doing so does not grant a meaningful security improvement.
    |
    */

    'cookie' => env(
        'SESSION_COOKIE',
        Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
    ),

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Path
    |--------------------------------------------------------------------------
    |
    | The session cookie path determines the path for which the cookie will
    | be regarded as available. Typically, this will be the root path of
    | your application, but you're free to change this when necessary.
    |
    */

    'path' => env('SESSION_PATH', '/'),

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | This value determines the domain and subdomains the session cookie is
    | available to. By default, the cookie will be available to the root
    | domain and all subdomains. Typically, this shouldn't be changed.
    |
    */

    'domain' => env('SESSION_DOMAIN'),

    /*
    |--------------------------------------------------------------------------
    | HTTPS Only Cookies
    |--------------------------------------------------------------------------
    |
    | By setting this option to true, session cookies will only be sent back
    | to the server if the browser has a HTTPS connection. This will keep
    | the cookie from being sent to you when it can't be done securely.
    |
    */

    'secure' => env('SESSION_SECURE_COOKIE'),

    /*
    |--------------------------------------------------------------------------
    | HTTP Access Only
    |--------------------------------------------------------------------------
    |
    | Setting this value to true will prevent JavaScript from accessing the
    | value of the cookie and the cookie will only be accessible through
    | the HTTP protocol. It's unlikely you should disable this option.
    |
    */

    'http_only' => env('SESSION_HTTP_ONLY', true),

    /*
    |--------------------------------------------------------------------------
    | Same-Site Cookies
    |--------------------------------------------------------------------------
    |
    | This option determines how your cookies behave when cross-site requests
    | take place, and can be used to mitigate CSRF attacks. By default, we
    | will set this value to "lax" to permit secure cross-site requests.
    |
    | See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value
    |
    | Supported: "lax", "strict", "none", null
    |
    */

    'same_site' => env('SESSION_SAME_SITE', 'lax'),
    'secure' => env('SESSION_SECURE_COOKIE', false),


    /*
    |--------------------------------------------------------------------------
    | Partitioned Cookies
    |--------------------------------------------------------------------------
    |
    | Setting this value to true will tie the cookie to the top-level site for
    | a cross-site context. Partitioned cookies are accepted by the browser
    | when flagged "secure" and the Same-Site attribute is set to "none".
    |
    */

    'partitioned' => env('SESSION_PARTITIONED_COOKIE', false),

];
```

### .env
```
SESSION_DRIVER=database
SESSION_DOMAIN=
#SESSION_DOMAIN=192.168.X.XX

#SANCTUM_STATEFUL_DOMAINS=http://localhost:3000,http://192.168.X.XX:3000
```

# SESSION_DRIVER=database について
SESSION_DRIVER=database にするとセッションをデータベースの[sessions]テーブルに保存されるようになる


## 役に立ったコマンド

### テーブルの操作
```
php artisan tinker
DB::table('sessions')->count();
auth()->loginUsingId(1); // 1番のユーザーで強制ログイン
auth()->check(); // true ならOK
auth()->user(); // ログインユーザー情報を取得
```

### 4️⃣ Laravelのキャッシュとセッションをクリア
すべてのキャッシュとセッションデータをクリアして、設定を再適用します。
```
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
```

### データベースに保存されているセッションを削除（リセット）します。
```
php artisan tinker
DB::table('sessions')->truncate();
```

### Laravelのバージョン確認コマンド
```
php artisan --version 
```

# Filamentに成功した後に
#### users テーブルを表示できるようにするコマンド
```
php artisan make:filament-resource User --generate
```

### 外部のデータベースのテーブルをFilamentで管理するために追加するコマンド
```
php artisan make:filament-resource DeshioKiroku
php artisan make:filament-resource Deshiouser
php artisan make:filament-resource Deshiostaff
php artisan make:filament-resource Deshiogendertype
```

依存関係の自動読み込み（autoload）が正しく設定されていない
```
composer dump-autoload
```

.envファイルの変更の反映
```
php artisan migrate
```

フィラメントインストールコマンド
```
composer require filament/filament
```

管理者パネルのセットアップ
```
php artisan filament:install
```

1. フィラメントのインストールコマンドを実行
```
php artisan filament:install --panels
```

管理者ユーザの作成
```
php artisan make:filament-user
```

カスタムページの作成
```
php artisan make:filament-page Dashboard
```

5.権限管理（Spatie Permissionの導入）
```
Filament でユーザーの権限を管理する
composer require spatie/laravel-permission
```

マイグレーションを実行し、ユーザーモデルを編集してHasRolesトレイトを追加します。
```
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
```

実行すべき手順
```
composer require filament/filament
composer dump-autoload
php artisan cache:clear
php artisan config:clear
php artisan filament:install:インストール
```

その後、設定を再読み込みしてください。
```
php artisan config:clear
```