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.
#Tried adding new fields for user profiles with no luck!
86 messages · Page 1 of 1 (latest)
We can’t help you unless you show code.
What do you want me to show case there is a lot of it...?
The relevant code? Where you’ve added the fields, how you’re then trying to save them, etc.
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...
Run dd($request->all()) in your controller to see what data is actually reaching your back end
Just at the beginning of the store method
What's the error message?
Did you forget the semicolon at the end of the dd statement?
I haven't commented the rest, or should I not do that?
No need to comment the rest of the code. dd returns the parameters to the browser and stops execution
ok, with or without commenting the other stuff I get nothing on front
I mean the page looks normal, no errors
You'll only see the dd results after submitting the form
Since it's on the store method
nothing after submitting
Define "nothing"
I mean I don't see anything on the page...no error, nothing
just the normal page
like before
Are you seeing the register page? Or some other page?
Oh the code on the controller you shared is from the register page
That's why it has User::create
ok, I have them on the profile controller
Show the profile controller code
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
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
The profile form submits to the
updatemethod on the ProfileController
So what you need to do is modify that method
this is what I have on the update function:
As well as the ProfileUpdateRequest class
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');
}```
Yeah pretty sure all you'll have to do is change the ProfileUpdatedRequest class
my question is, why is there no mention of the name field here?
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()
do I need to just copy the validate code and add the validate method?
No read up on this
https://laravel.com/docs/9.x/validation#form-request-validation
All you'll need to do is edit the existing form request class and everything will work as intended
without moving the code from store to update?
No just get rid of that store method entirely
Thank you for your help so far, I will follow your suggestion when I get back home from work.
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 😄
If you look above I posted this code
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,
No hes saying he posted the $fillable code from the model
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
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.
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>
@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 .
but why is the description one not staying with the "old" value??
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
but I need it that way, and what about the other fields that work?
users should see what they posted before doing any edits
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
what do you mean directly? I am accessing the edit page, or do you mean the /profile/edit or something?