0

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, email, 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?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 4
    set password field default value to null in database table – John Lobo Jun 19 '21 at 14:24
  • Are you meant to be creating a new User? – Rwd Jun 19 '21 at 14:29
  • 1
    you are not updating , you are creating new user , $user = new User(); so what do you want to do here? update or create new user? – sherifcoder Jun 19 '21 at 14:36
  • 1
    A few helpful tips for asking questions here: (1) try things before you ask a question. I wonder, given your committed rate of asking questions, that you are not working through a problem enough on your own before asking. (2a) The boilerplate phrases "I would really appreciate any idea or suggestion from you guys" and "Thanks in advance" can be omitted from all posts. They will generally be removed in time, and adding them just creates work for volunteer editors. (3b) Stack Overflow is not a chat-room. (4) Post titles don't need a home-made tag - that is what the tagging system is for. – halfer Jun 20 '21 at 10:20
  • Do have a browse on [Meta Stack Overflow](https://meta.stackoverflow.com/) when you get a chance - and of course the [Help Centre](https://stackoverflow.com/help). They both are valuable sources of guidance about posting here. – halfer Jun 20 '21 at 10:21

1 Answers1

1

If you are using migration then you can set field to nullable.

   $table->string('password')->nullable();

then rerun migration.

If you are not using then you can set in mysql using phpmyadmin.

enter image description here

If you are thinking to alter using query then

ALTER TABLE users MODIFY password varchar(191) DEFAULT NULL
John Lobo
  • 14,355
  • 2
  • 10
  • 20