mercredi 16 septembre 2020

Laravel Integrity constraint violation: 1452

Hi I am trying to developer a human resources management system. I have to tables users (employees and admins) and one for departments which are connected by a department_id many to many relationships. Now I am building CRUD for employees. I have two problems 1)When I press edit for the employee I only get one department name and not all departments name 2)After I edit name or salary or anything else I get this error

I think I have done something wrong in select option but im not sure what it is Any help would be appreciated

Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dlaravel`.`users`, CONSTRAINT `users_department_id_foreign` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE CASCADE) (SQL: update `users` set `image` = 1600250154.jpg, `salary` = 700, `department_id` = 0, `users`.`updated_at` = 2020-09-16 09:55:54 where `id` = 5)

Tables:

Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();

            $table->string('image',255)->nullable();
            $table->double('salary')->nullable();;
            $table->boolean('is_admin')->default(0);
            $table->foreignId('department_id')->nullable()->constrained()->onDelete('cascade');

            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

 Schema::create('departments', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('parent_id')->default(0);
            $table->timestamps();
        });

User model

public function department()
    {
        return $this->BelongsTo('App\Models\Department');
    }

Department model public function employees()

    {
        return $this->hasMany('App\Models\User');
    }

    public function hierarchy() {
        return $this->hasMany('App\Models\Department', 'parent_id');
    }
}

Controller

public function edit($id)
    {
        $employee = User::findOrFail($id);
        return view('admin.editEmployees')->with('employee',$employee);
    }
 public function update(Request $request, $id)
    {
        $user = User::find($id);

        $user -> name = $request -> input('name');
        $user->email = $request->input('email');
        $user->salary = $request->input('salary');
        $user->department_id = $request->input('parent_id');
    
        if($request->hasFile('image'))
          {
             $file = $request->file('image');
             $extension = $file->getClientOriginalExtension(); //getting image extension
             $filename = time() . '.' . $extension;
             $file->move('images/', $filename);
             $user->image = $filename;
          } 
    
        $user->update();
    
        return redirect('dashboard')->with('status','Employee record is updated');
    }

editEmployee.phh

<form role="form" action="/dashboard/" method="POST" enctype="multipart/form-data">
                  
                  
                <div class="card-body">
                  <div class="form-group">
                    <label for="exampleInputText">Name</label>
                    <input type="text" class="form-control" id="exampleInputText" name="name" value="">
                  </div>
                  <div class="form-group">
                    <label for="exampleInputEmail1">Email</label>
                    <input type="email" name="email" class="form-control" id="exampleInputPassword1" value="">
                  </div>
                  <div class="form-group">
                    <label for="exampleInputText">Salary</label>
                    <input type="text" class="form-control" id="exampleInputText" name="salary" value="">
                  </div>
                  <div class="form-group">
                    <label for="dependency">Dependent on</label>
                    <select class="form-control" name="parent_id" id="dependency">
                      <option value="0"></option>
                      
                        @foreach($employee as $employees)
                          <option value=""></option>
                         @endforeach
                 
                    </select>
                    </div>
                  <div class="form-group">
                    <label for="exampleInputFile">File input</label>
                    <div class="input-group">
                      <div class="custom-file">
                        <input type="file" name="image" class="custom-file-input" id="exampleInputFile">
                        <label class="custom-file-label" for="exampleInputFile"></label>
                      </div>
                    </div>
                  </div>
                </div>
                <!-- /.card-body -->

                <div class="card-footer">
                  <button type="submit" class="btn btn-success">Update</button>
                  <a href="/dashboard" class="btn btn-danger">Cancel</a>
                </div>
              </form>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire