【Laravel】GuestLoginの場合、Profileを編集不可にする手順

# ModelにisGuest()を追記
```/app/Models/User.php
<?php

namespace App\Models;

class User extends Authenticatable
{

    ~~~

    // ゲストログイン
    public function isGuest()
    {
    return $this->email === 'guest@example.com';
    }
}

```


# viewsを編集
## タイトルに編集不可であることを追記
```/views/profile/edit.blade.php
{{ __('Profile') }} @if(Auth::user()->isGuest()) (GuestLoginの場合、編集不可) @endif
```
![スクリーンショット 2025-04-16 12.19.10.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3726068/4767cb95-6739-4a07-bed3-c499b207b094.png)


## 「update-profile-information-form.blade.php」の「name」と「email」を編集不可にする
### inputの編集
「:readonly="Auth::user()->isGuest()"」を追記する
↓
```/views/profile/partials/update-profile-information-form.blade.php
# name
<x-text-input id="name" name="name" type="text" class="mt-1 block w-full" :value="old('name', $user->name)" required autofocus autocomplete="name" 
    :readonly="Auth::user()->isGuest()" />

# email
<x-text-input id="email" name="email" type="email" class="mt-1 block w-full" :value="old('email', $user->email)" required autocomplete="username"
    :readonly="Auth::user()->isGuest()" />
```

### buttonの編集
```/views/profile/partials/update-profile-information-form.blade.php
# 編集前
{{-- <div class="flex items-center gap-4">
    <x-primary-button>{{ __('Save') }}</x-primary-button>

    @if (session('status') === 'profile-updated')
        <p
            x-data="{ show: true }"
            x-show="show"
            x-transition
            x-init="setTimeout(() => show = false, 2000)"
            class="text-sm text-gray-600"
        >{{ __('Saved.') }}</p>
    @endif
</div> --}}

# 編集後
<div class="flex items-center gap-4">
    @if(Auth::user()->isGuest())
        <x-primary-button disabled class="opacity-50 cursor-not-allowed">
            {{ __('Save') }}
        </x-primary-button>
    @else
        <x-primary-button>
            {{ __('Save') }}
        </x-primary-button>
    @endif

    @if (session('status') === 'profile-updated')
        <p
            x-data="{ show: true }"
            x-show="show"
            x-transition
            x-init="setTimeout(() => show = false, 2000)"
            class="text-sm text-gray-600"
        >{{ __('Saved.') }}</p>
    @endif
</div>    
```
![スクリーンショット 2025-04-16 12.24.11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3726068/945e4a4a-3783-489c-900e-0a25fc7b8a9f.png)


## 「update-password-form.blade.php」の「current_password」と「password」と「password_confirmation」を編集不可にする
### inputの編集
「:readonly="Auth::user()->isGuest()"」を追記する
```/views/profile/partials/update-password-form.blade.php
# current-password
<x-text-input id="current_password" name="current_password" type="password" class="mt-1 block w-full" autocomplete="current-password"
    :readonly="Auth::user()->isGuest()" />

# password
<x-text-input id="password" name="password" type="password" class="mt-1 block w-full" autocomplete="new-password"
                :readonly="Auth::user()->isGuest()" />

# password_confirmation
<x-text-input id="password_confirmation" name="password_confirmation" type="password" class="mt-1 block w-full" autocomplete="new-password"
                :readonly="Auth::user()->isGuest()" />
```

### buttonの編集
```/views/profile/partials/update-password-form.blade.php
# 編集前
{{-- <div class="flex items-center gap-4">
    <x-primary-button>{{ __('Save') }}</x-primary-button>

    @if (session('status') === 'password-updated')
        <p
            x-data="{ show: true }"
            x-show="show"
            x-transition
            x-init="setTimeout(() => show = false, 2000)"
            class="text-sm text-gray-600"
        >{{ __('Saved.') }}</p>
    @endif
</div> --}}

# 編集後
<div class="flex items-center gap-4">
    @if(Auth::user()->isGuest())
        <x-primary-button disabled class="opacity-50 cursor-not-allowed">
            {{ __('Save') }}
        </x-primary-button>
    @else
        <x-primary-button>
            {{ __('Save') }}
        </x-primary-button>
    @endif

    @if (session('status') === 'password-updated')
        <p
            x-data="{ show: true }"
            x-show="show"
            x-transition
            x-init="setTimeout(() => show = false, 2000)"
            class="text-sm text-gray-600"
        >{{ __('Saved.') }}</p>
    @endif
</div>
```
![スクリーンショット 2025-04-16 12.28.14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3726068/2bb732d2-3909-4875-a9d7-74293b702094.png)



## 「delete-user-form.blade.php」の「button2箇所」を編集不可にする
### 1箇所目の編集
```/views/profile/partials/delete-user-form.blade.php
# 編集前
{{-- <x-danger-button
    x-data=""
    x-on:click.prevent="$dispatch('open-modal', 'confirm-user-deletion')"
>{{ __('Delete Account') }}</x-danger-button> --}}

# 編集後
@if(Auth::user()->isGuest())
    <x-danger-button disabled class="opacity-50 cursor-not-allowed">
        {{ __('Delete Account') }}
    </x-danger-button>
@else
    <x-danger-button
        x-data=""
        x-on:click.prevent="$dispatch('open-modal', 'confirm-user-deletion')"
    >
        {{ __('Delete Account') }}
    </x-danger-button>
@endif
```
![スクリーンショット 2025-04-16 12.35.14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3726068/9c9e53b6-83c7-4883-8fc2-6b79f69172bd.png)


### 2箇所目の編集
こちらは1箇所目を修正していれば表示されることはないが、念のために修正する
```/views/profile/partials/delete-user-form.blade.php
# 編集前
{{-- <x-danger-button class="ml-3">
    {{ __('Delete Account') }}
</x-danger-button> --}}

# 編集後
@if(Auth::user()->isGuest())
    {{-- 表示はされるが無効化 --}}
    <x-danger-button class="ml-3 opacity-50 cursor-not-allowed" disabled>
        {{ __('Delete Account') }}
    </x-danger-button>
@else
    {{-- 通常ユーザー用 --}}
    <x-danger-button type="submit" class="ml-3">
        {{ __('Delete Account') }}
    </x-danger-button>
@endif
```
![スクリーンショット 2025-04-16 12.35.49.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3726068/e21412e4-1968-4584-8fa0-33a9209a6447.png)



# 参考：GuestLoginの実装
https://qiita.com/honaki/items/d46c9711d41b0e2482c3