Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brownie's avatar

Adding a checkbox to Laravel Breeze profile page

Hi all, very new to Laravel but eager to learn!

I have a new install of Laravel Breeze using Tailwind and I'm trying to add fields to the profile page. I've searched high and low to try and find a solution but I'm stuck :(

The installation came with the following fields: Name, Email

All fine, I've since added the following using artisan migration Surname (varchar), Marketing (boolean / tinyint(1)

The suname field saves with no issue however the marketing boolean is refusing to save and I mean REFUSING.

Steps taken: I've created a cast in the User model for the field, I've done a dd which is showing me the value of 1 being submitted in the form, I've tried changing the field to varchar, I've googled for what feels like an eternity

I'm new to Laracasts too so appologies if I've not done this right or posted in the incorrect place!

My Laravel version is 11.

Thanks!

0 likes
4 replies
tykus's avatar

Did you make surname fillable on the User model, but not the marketing property? What, if any, error messages are you receiving?

It would help us if you shared the relevant Model, Controller (and perhaps view template) code.

1 like
brownie's avatar

Hi tykus,

Thanks for replying!

My User Model hase the following:

    protected $fillable = [
        'name',
        'surname',
        'email',
        'password',
        'marketing',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
            'marketing' => 'integer',
        ];
    }
}

My ProfileController has the following:

 public function update(ProfileUpdateRequest $request): RedirectResponse
    {

        if ($request->user()->isDirty('email')) {
            $request->user()->email_verified_at = null;
        }
        
        $request->user()->save();
        return Redirect::route('profile.edit')->with('status', 'profile-updated');
    }

My ProfileUpdate request has the following:

public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'surname' => ['required', 'string', 'max:255', 'min:3'],
            'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
        ];
    }

My profile view has the following:

<div class="block mt-4">
            <label for="marketing" class="inline-flex items-center">
                <input id="marketing" type="checkbox" value=1 class="rounded border-gold text-gold shadow-sm focus:ring-gold" name="marketing">
                <span class="ms-2 text-sm text-gray-600">{{ __('Marketing') }}</span>
            </label>
        </div>

Let me know if there's anything else I need to provide :)

Thanks!

tykus's avatar
tykus
Best Answer
Level 104

In the Controller action, did you delete this line which fills the Model with the form data?

$request->user()->fill($request->validated());

Secondly, if an input key is not included in the rules array, then it is not included in the validated data, so update the ProfileUpdateRequest to include marketing in the validation rules even if it is an empty rule, it needs to be included:

public function rules(): array
{
    return [
        'name' => ['required', 'string', 'max:255'],
        'surname' => ['required', 'string', 'max:255', 'min:3'],
        'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
        'marketing' => '',
    ];
}

However, since checkboxes are somewhat special (they are not included in the Request payload whenever unchecked), you need an additional step to check if it is present. This can be achieve in a number of different ways, but I like the following whenever using FormRequest classes:

// ProfileUpdateRequest
    protected function prepareForValidation(): void
    {
        $this->merge([
            'marketing' => $this->has('marketing'),
        ]);
    }

The has method returns a Boolean which determines if the given key is in the Request - ultimately, was the checkbox checked or unchecked!

Lastly, in the view template, you can show the state of the User property using a ternary to set a checked attribute on the element:

<input id="marketing" 
        type="checkbox" 
        value=1 
        class="rounded border-gold text-gold shadow-sm focus:ring-gold" 
        name="marketing" 
        {{ $user->marketing ? 'checked' : '' }}
>
1 like
brownie's avatar

@tykus Sir, you are wonderful. I had accidently deleted the validation when messing around with it trying to get it to work but the additionals you gave me, the validation and the prepare function are simply amazing and now it is working.

Thanks again and have a great weekend :)

1 like

Please or to participate in this conversation.