0

I am having issues inserting data into my database tables. I have users table which is has a relation of one to many to school table and region table. That is, users table has foreign keys of school and region tables.

I have created the relations correctly but then when i try to insert values into the database, i get the error.

PS: I have one user(admins) in the user table (the school_id and region_id was inserted accurately) already which i saved through RegisterController. Now, when i sign in with the user(admin) already saved, i want to save other users through the UserController but then i get the error above.

What am i not doing right?. I am only a beginner with database relationship and laravel

General error: 1364 Field 'school_id' and `region_id`doesn't have a default value 

meaning the field cannot be empty.

UserController

 public function store(UserFormRequest $request)
    {
        $user = new User(array(
             'name' => $request->get('name'),
             'email' => $request->get('email'),
             'password'=> $request->get('password'),
             'phone' => $request->get('phone')           

              ));
        $user->save();

    }

2 Answers 2

1

there are three solutions to your problem.

1

 public function store(UserFormRequest $request)
    {
        $user = new User(array(
             'name' => $request->get('name'),
             'email' => $request->get('email'),
             'password'=> $request->get('password'),
             'phone' => $request->get('phone'),
             'school_id'=>'you must pass value here',
             'region_id'=>'you must pass value here'        

              ));
        $user->save();

    }

2 Go to phpmyadmin make your school_id,region_id default value NULL. so you are good to go.

Sign up to request clarification or add additional context in comments.

Comments

1

MySQL is telling you that those fields can't be empty because they aren't nullable.

To make those fields nullable() in your users migration:

// Your create users table migration
$table->integer('school_id')->nullable();
$table->integer('region_id')->nullable();

(You can add any other properties to these - like index(), unsigned(), or foreign keys, etc.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.