windows docker WSL2 sail laravel filament phpadmin アプリ構築 メモ

windows docker WSL2 sail laravel filament phpadmin アプリ構築 メモ

アプリ構築メモ

前提

windows 11
docker desktop 4.12(ubuntu使用ON、WSL2使用ON)
WSL2 (ubuntu)
vs code

laravel インストール

curl -s https://laravel.build/sales | bash
cd sales
code .

.env 編集

APP_LOCALE=ja
APP_FALLBACK_LOCALE=ja
APP_FAKER_LOCALE=ja_JP

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=sales
DB_USERNAME=sales
DB_PASSWORD=password

docker-compose.yml の編集

    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        links:
            - mysql:mysql
        ports:
            - 8080:80
        environment:
            MYSQL_USERNAME: '${DB_USERNAME}'
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            PMA_HOST: mysql
        networks:
            - sail

起動

sail up -d
sail artisan migrate

http://localhost/

filamentインストール

sail composer require filament/filament:"^3.2" -W

filamentのPanel Builderをインストール

sail artisan filament:install --panels

admin、Yesと入力

filament用管理ユーザ作成

sail artisan make:filament-user

admin
admin@test.co.jp
password

と入力

モデル作成

sail artisan make:model Dept -m
sail artisan make:model Emp -m

create table

yyyy_mm_dd_140749_create_depts_table.php
    public function up(): void
    {
        Schema::create('depts', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }
yyyy_mm_dd_140755_create_emps_table.php
    public function up(): void
    {
        Schema::create('emps', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->date('date_of_birth');
            $table->foreignId('depts_id')->constrained('depts')->cascadeOnDelete();
            $table->timestamps();
        });
    }
sail artisan migrate

モデル

Dept.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Dept extends Model
{
    //
    use HasFactory;

    protected $fillable = ['name'];  //追加

    public function emps(): HasMany
    {
        return $this->hasMany(Emp::class);
    }
}

Emp.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Emp extends Model
{
//
use HasFactory;

protected $fillable = ['name' ,'email', 'date_of_birth', 'depts_id']; //追加

public function depts(): BelongsTo
{
return $this->belongsTo(Dept::class);
}
}



filament用管理リソースを作成

sail artisan make:filament-resource Dept
sail artisan make:filament-resource Emp
DeptResource.php
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\DeptResource\Pages;
use App\Filament\Resources\DeptResource\RelationManagers;
use App\Models\Dept;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class DeptResource extends Resource
{
    protected static ?string $model = Dept::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                //
                Forms\Components\TextInput::make('name')
                    ->required()        // 入力必須
                    ->maxLength(255),   // 最長255文字
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
                Tables\Columns\TextColumn::make('name')
                    ->sortable()
                    ->searchable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListDepts::route('/'),
            'create' => Pages\CreateDept::route('/create'),
            'edit' => Pages\EditDept::route('/{record}/edit'),
        ];
    }
}


EmpResource.php
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\EmpResource\Pages;
use App\Filament\Resources\EmpResource\RelationManagers;
use App\Models\Emp;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class EmpResource extends Resource
{
    protected static ?string $model = Emp::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                //
                Forms\Components\TextInput::make('name')
                    ->required()        // 入力必須
                    ->maxLength(255),   // 最長255文字
                Forms\Components\TextInput::make('email')
                    ->required()        // 入力必須
                    ->maxLength(255),   // 最長255文字
                // 日付ピッカー
                Forms\Components\DatePicker::make('date_of_birth')
                    ->required()        // 入力必須
                    ->maxDate(now()),   // 最大日付:現在
                Forms\Components\Select::make('depts_id')
                    // モデル:Patient::owner() / ラベル:name
                    ->relationship('depts', 'name')
                    ->searchable()  // 検索対象に設定
                    ->preload()     // 事前に取得50件
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
                Tables\Columns\TextColumn::make('name')
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('email')
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('date_of_birth')
                    ->sortable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('depts.name')
                    ->sortable()
                    ->searchable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListEmps::route('/'),
            'create' => Pages\CreateEmp::route('/create'),
            'edit' => Pages\EditEmp::route('/{record}/edit'),
        ];
    }
}

参考)

https://macocci7.net/blog/2024/05/23/%E3%80%90laravel11-filament3%E3%80%91panel-builder-%E3%83%86%E3%83%A5%E3%83%BC%E3%83%88%E3%83%AA%E3%82%A2%E3%83%AB%E5%AE%9F%E8%B7%B5%E8%A8%98%E9%8C%B2