Home > Blockchain >  error when updating a column in database - Integrity constraint violation: Column 'xxxxxxxx
error when updating a column in database - Integrity constraint violation: Column 'xxxxxxxx

Time:11-26

I'm new to the subject, I have a dropdown that fetches data from a table in the database, when I select another option and save, give me: Integrity constraint violation: 1048 Column 'department_id' cannot be null (SQL: update users set department_id = ?, users.updated_at = 2021-11-25 09:41:43 where id = 1)

My Index:

<div class="form-group mb-6">
    <label class="form-label">{{ $trans('labels.department') }}</label>
    <select class="form-select"  v-model="form.department_id">
      <option :value="department.id" v-for="department in $page.departments">
        <p class="mt-1 text-sm leading-5 text-gray-500">{{ department.name }}</p>
      </option>
    </select>
</div>

My Controller:


 public function update(Request $request)
    {
        $this->validate($request, [
            'name'  => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,' . auth()->user()->id]
        ]);

        auth()->user()->update([
            'name'  => $request->input('name'),
            'email' => $request->input('email'),
            'department_id' => $request->input('department_id'),
        ]);

        if ($request->hasFile('profile_picture')) {
            $media = MediaUploader::fromSource($request->file('profile_picture'))
                ->toDestination('public', 'avatars')
                ->onDuplicateIncrement()
                ->useHashForFilename()
                ->beforeSave(function ($media) {
                    $media->uuid = Str::uuid();
                })
                ->upload();

            auth()->user()->attachMedia($media, 'avatars');
        }

        session()->flash('message', __('app.messages.profile-updated'));
        
        return back();
    }

USER Model:


protected $hidden = [
        'password', 'remember_token',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($user) {
            $user->uuid = Str::uuid();
        });
    }

    public function tenant()
    {
        return $this->belongsTo(Tenant::class);
    }

    public function department()
    {
        return $this->belongsToMany(Department::class);
    }

    public function favoriteProjects()
    {
        return $this>belongsToMany(Project::class,'favorite_project_user');
    }

    public function isAdmin()
    {
        return $this->role === User::ROLE_ADMIN;}

    public function isTenantUser()
    {
        return $this->role === User::ROLE_TENANT_USER;
    }
    public function cacheKey($key)
    {
        return auth()->user()->uuid . '.' . $key;
    }

The column to update is department_id which is a foreignKey in User table

CodePudding user response:

in your User Model, make sure you have $fillable property with correct properties:

class User extends Authenticatable
{
 protected $fillable = ['name','email','department_id','role'];
....
}

CodePudding user response:

the problem was in my index, I forgot to add the department_id to the data(),


<script>
    import VAppDefaultLayout from '@/views/back/app/layouts/default'
    import VNavMenu from '@/views/back/app/account/nav-menu'
    import VAlert from '@/components/alert'
    import VInputFile from '@/components/input-file'
    import Form from '@/utils/form'

    export default {
        metaInfo() {
            return {
                title: this.$trans('headings.edit-profile')
            }
        },

        components: {
            VAppDefaultLayout,
            VNavMenu,
            VAlert,
            VInputFile
        },

        data() {
            return {
                form: new Form({
                    name: this.$page.user.name,
                    email: this.$page.user.email,
                    department_id: this.$page.user.department_id,
                    profile_picture: null
                })
            }
        },

        methods: {
            submit() {
                this.form.processing = true;

                let data = new FormData();
                data.append('name', this.form.name);
                data.append('email', this.form.email);
                data.append('department_id', this.form.department_id);
                data.append('profile_picture', this.form.profile_picture);
                data.append('_method', 'PUT');

                this.$inertia.post(route('app:profile.update'), data)
                    .then(() => this.form.processing = false);
            }
        }
    }
</script>

  • Related