I'm making a CMS with Laravel 8 and in this CMS, I have made a page for editing user profile information.
So here is the form:
<form method="POST" action="{{ route('profile.info.confirm') }}">
@csrf
<label class="col-lg-2 control-label" for="name">Name</label>
<input id="name" name="name" type="text" class="form-control" value="{{ auth()->user()->name }}">
<label class="col-lg-2 control-label " for="email">Email</label>
<input id="email" name="email" type="text" class="form-control" value="{{ auth()->user()->email }}">
<label class="col-lg-2 control-label " for="email">Profile Pic</label>
<input id="avatar" name="avatar" type="file" class="email form-control">
<label class="col-lg-2 control-label " for="email"></label>
@if(Auth::user()->photo_id)
<img src="{{ Auth::user()->photo->path }}" width="50" height="50" alt="user-img" title="{{ Auth::user()->photo->name }}" class="img-circle img-thumbnail img-responsive">
@else
<p>You don't have any profile picture</p>
@endif
<button class="btn btn-primary">
UPDATE
</button>
</form>
Then at the Controller:
public function postProfileInfo(Request $request)
{
$data = $request->validate([
'name' => 'required',
'email' => 'required',
'avatar' => 'mimes:jpeg,jpg,png,gif|max:10000|nullable'
]);
$user = new User();
if($file = $request->file('avatar')){
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = new Photo();
$photo->name = $file->getClientOriginalName();
$photo->path = $name;
$photo->user_id = Auth::id();
$photo->save();
$user->photo_id = $photo->id;
}
$user->name = $data['name'];
$user->email = $data['email'];
$user->save();
return redirect('/admin');
}
Now the problem is whenever I try to submit the form, I get this error message:
Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into
users
(name
,updated_at
,created_at
)
Basically, I don't want to update the password, I just want to update name
, email
& avatar
(optional).
So what is going wrong here?