#Tried adding new fields for user profiles with no luck!

86 messages · Page 1 of 1 (latest)

royal garnet
#

Hello, I have tried again and again to figure this out, but can't for the love of God...I'm very frustrated.
Is there any tutorial about this somewhere cause I can't find one. I read the docs and searched a lot and still don't know what's wrong.
The fields are present on the database but when I enter data from the form the database does not update with the data. I'm using the breeze scaffolding.

glad flower
royal garnet
#

What do you want me to show case there is a lot of it...?

glad flower
#

The relevant code? Where you’ve added the fields, how you’re then trying to save them, etc.

royal garnet
#

I upload files or are there other ways?

#
I guess this works?
#

the fields are in this blade:

#
<form method="post" action="{{ route('profile.update') }}" class="mt-6 space-y-6">
      @csrf
      @method('patch')

      <div>
          <x-input-label for="username" :value="__('Nume Utilizator')" />
          <x-text-input id="username" name="username" type="text" class="mt-1 block w-full" :value="old('username', $user->username)" required autocomplete="username" />
          <x-input-error class="mt-2" :messages="$errors->get('username')" />
      </div>

      <div>
        <x-input-label for="description" :value="__('Descriere Personală')" />
        <x-text-input id="description" name="description" type="text" class="mt-1 block w-full" :value="old('description', $user->description)" required autocomplete="description" />
        <x-input-error class="mt-2" :messages="$errors->get('description')" />
    </div>

    <div>
      <x-input-label for="siteweb" :value="__('Site Personal')" />
      <x-text-input id="siteweb" name="siteweb" type="url" class="mt-1 block w-full" :value="old('siteweb', $user->username)" required autocomplete="siteweb" />
      <x-input-error class="mt-2" :messages="$errors->get('siteweb')" />
  </div>

      <div class="flex items-center gap-4">
          <x-primary-button>{{ __('Salvează') }}</x-primary-button>

          @if (session('status') === 'profile-updated')
              <p
                  x-data="{ show: true }"
                  x-show="show"
                  x-transition
                  x-init="setTimeout(() => show = false, 5000)"
                  class="text-sm text-gray-600"
              >{{ __('Setări salvate!') }}</p>
          @endif
      </div>
  </form>
#

did this in the user model:

#
protected $fillable = [
        'name',
        'email',
        'password',
        'avatar',
        'username',
        'descriprion',
        'siteweb',
    ];
#

this in the profile controller:

#
public function store(Request $request)
    {
        $request->validate([
            'username' => ['required', 'text', 'max:255'],
            'description' => ['required', 'text', 'max:255'],
            'siteweb' => ['required', 'url', 'max:255'],
            'avatar' => 'required|image',
        ]);

        $user = User::create([
            'username' => $request->username,
            'description' => $request->description,
            'siteweb' => $request->siteweb,
        ]);

        Auth::login($user);
#

other than making a migration and then migrate, nothing else, I think

#

I get no errors

#

first I tried to copy like the other fields work from the scaffolding, mainly the name field but can't seem to find where does this field update or create...

tranquil hearth
#

Run dd($request->all()) in your controller to see what data is actually reaching your back end

royal garnet
#

where exactly to place that...sorry, very new at this

#

in that store function?

tranquil hearth
#

Just at the beginning of the store method

royal garnet
#

ok

#

I just get a 500 error

tranquil hearth
#

What's the error message?

#

Did you forget the semicolon at the end of the dd statement?

royal garnet
#

I haven't commented the rest, or should I not do that?

tranquil hearth
#

No need to comment the rest of the code. dd returns the parameters to the browser and stops execution

royal garnet
#

ok, with or without commenting the other stuff I get nothing on front

#

I mean the page looks normal, no errors

tranquil hearth
#

You'll only see the dd results after submitting the form

#

Since it's on the store method

royal garnet
#

nothing after submitting

tranquil hearth
#

Define "nothing"

royal garnet
#

I mean I don't see anything on the page...no error, nothing

#

just the normal page

#

like before

tranquil hearth
#

Are you seeing the register page? Or some other page?

royal garnet
#

the profile page

#

the fields are on the profile page

tranquil hearth
#

Oh the code on the controller you shared is from the register page

#

That's why it has User::create

royal garnet
#

ok, I have them on the profile controller

tranquil hearth
#

Show the profile controller code

royal garnet
#

the message was to long

tranquil hearth
#

No worries. So it seems like you've been accidently writing code in files meant for the register form when you meant to write code for the profile form

#

The profile form submits to the update method on the ProfileController

royal garnet
#

this was not like it is right now...I copied some code from there thinking would help...I had it without the create and still didn't work

tranquil hearth
#

The profile form submits to the update method on the ProfileController
So what you need to do is modify that method

royal garnet
#

this is what I have on the update function:

tranquil hearth
#

As well as the ProfileUpdateRequest class

royal garnet
#
public function update(ProfileUpdateRequest $request)
    {
        $request->user()->fill($request->validated());

        if ($request->user()->isDirty('email')) {
            $request->user()->email_verified_at = null;
        }

        $request->user()->save();

        return Redirect::route('profile.edit')->with('status', 'profile-updated');
    }```
tranquil hearth
#

Yeah pretty sure all you'll have to do is change the ProfileUpdatedRequest class

royal garnet
#

my question is, why is there no mention of the name field here?

tranquil hearth
#

In this case all of the validation logic is handled in the request class. Then all the validated data is retrieved on the first line of that function

#

With $request->validated()

royal garnet
#

do I need to just copy the validate code and add the validate method?

tranquil hearth
#

All you'll need to do is edit the existing form request class and everything will work as intended

royal garnet
#

without moving the code from store to update?

tranquil hearth
#

No just get rid of that store method entirely

royal garnet
#

Thank you for your help so far, I will follow your suggestion when I get back home from work.

covert kettle
#

Also, make sure you have added the new field on the fillable array of the model. Most of the time that will be the culprit of this issue 😄

royal garnet
covert kettle
#

So you haven't solved this?
@tranquil hearth gave you the answer.

$request->validated()

This method only returns the field that you have validated in the form request, in your case, ProfileUpdateRequest form request.
You probably have not added the missing field in that form request validation rule,

tranquil hearth
covert kettle
#

ah okay

#

I was just mentioning that fillable is easily missable place too 😀

royal garnet
#

Thank you @tranquil hearth and @covert kettle for the help, my profile works now, but still have a problem with one field, the description field, which had a default state "text", copied from some tutorial and did the migration and now when ever I add content to it, it defaults to "text"

#

I removed it from the database, put null and now it does not populate data to it, not even the "text" string

covert kettle
#

need a little bit more context.
The description field of the users table? and the same update method?
Another useful method of debuging in laravel is the use of dd() and dump() helper methods
eg:dd($request->validated()) just above where you are saving/updating.
This will let you quickly see what data is going to be saved.

royal garnet
#

yes to the questions

#

I get this with dd:

#
  "username" => "laurentiu86stan"
  "description" => "test text"
  "siteweb" => "https://laurentiustan.ro"
]```
#

all data seems to be saved

#

I discovered a typo on the User model, it was descripion on the fillable

#

now the text goes to the database and prints, but does not stay with the old method

#

is this good? <div> <x-input-label for="description" :value="__('Descriere Personală')" /> <textarea id="description" name="description" type="text" class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm" value="{{ old('description', $user->description) }}" required autocomplete="description"></textarea> <x-input-error class="mt-2" :messages="$errors->get('description')" /> </div>

covert kettle
#

@royal garnet
if that is an update-specific blade file, then that is good if not, and the $user object might not be set then you should probably do an isset check $user->description ?? null .

royal garnet
#

All other fields are there too

#

And they work as intended

royal garnet
#

but why is the description one not staying with the "old" value??

covert kettle
#

it is because you redirected away from the old request so it gets cleared away.
and it is also not a good practice to rely on old to repopulate the database data in the form of edit page

royal garnet
#

but I need it that way, and what about the other fields that work?

#

users should see what they posted before doing any edits

covert kettle
#

but what happens when you tried to access the edit page directly? There won't be any old data before that.
so old('description', $user->description) this is the correct way

#

it handles during the validation error and also first page open

royal garnet
#

what do you mean directly? I am accessing the edit page, or do you mean the /profile/edit or something?