3

here's the error

SQLSTATE[HY000]: General error: 1364 Field 'department_id' doesn't have a default value (SQL: insert into ms_user (name, username, role, email, password, updated_at, created_at)

ms_user model

protected $fillable = [
        'department_id','name', 'email', 'password','username','role',
    ];

create function :

    {
        return ms_user::create([
            'name' => $data['name'],
            'username' => $data['username'],
            'role' => $data['role'],
            'email' => $data['email'],
            'department_id' => $data['department_id'],
            'password' => bcrypt($data['password'])
        ]);
    }

validator function :

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'username' => 'required|string|max:255',
            'role' => 'required|in:user,admin',
            'department_id' => 'required|string',
            'email' => 'required|string|email|max:255|unique:ms_user',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

department_id is a dropdown menu that contains data from the ms_department table, department_id becomes the foreign key in the ms_user table and as the primary key in the ms_department

Regolith
  • 2,944
  • 9
  • 33
  • 50
daris dzakwan
  • 31
  • 1
  • 2
  • Hello there, first I haven't worked with either MySQL and Laravel (I use postgresql all the time), but based on your error, can you look into this https://stackoverflow.com/questions/15438840/mysql-error-1364-field-doesnt-have-a-default-values question and see whether it answer yours? – mfakhrusy Nov 01 '19 at 04:05

3 Answers3

0

It seam that your are trying to create a user with the filed department_id set to null. In your controller it seams your are passing $data['department_id'] I presume that value comme from the Request Object. But as that field doesn't have any value, when you the code to create user is execute, It send and empty value to that database. The table which contain users data doesn't allow that field to be empty.

You solve that by updating you migration to allow null on the departement_field like this

$table->integer('department_id')->nullable();

Or you can check if $data['department_id'] isn't null but using debug($data) in your controller.

Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31
0

it seems you are trying to make a bulk upload if not the name attribute of the input referring to department_id is empty or not correctly spelled

I had the same issue but the error in my case originated from my controller where I interchanged the names of my field living out exp_date

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
0

I had the same problem. I checked the stream on which data is passed in.

  1. Check if everything on view is ok.
  2. Check if everything on controller is fine.
  3. Check if field exists in model .
  4. Check if field exists in migration.

For me, I had put a . dot on my model. Like

protected $fillable = [
   'department_id'.'name', 'email', 'password','username','role',
];

between department_id and name.

The spellings are also important. Since there is no data from the view, I can't say for sure - but check if everything is correct.

ahuemmer
  • 1,653
  • 9
  • 22
  • 29
Hilo
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 21 '22 at 18:45