我在将数据插入数据库表时遇到问题。I have users table
which is 与school table
and有一对多的关系region table
。也就是说,users table
拥有foreign keys
的school and region tables
。
我已经正确创建了关系,但是当我尝试将值插入数据库时,出现错误。
PS:我已经通过RegisterController 保存了用户表中的一个用户(管理员)(已准确插入了school_id 和region_id)。现在,当我使用已保存的用户(管理员)登录时,我想通过 UserController 保存其他用户,但随后出现上述错误。
我做错了什么?。我只是数据库关系和laravel的初学者
General error: 1364 Field 'school_id' and `region_id`doesn't have a default value
意味着该字段不能为空。
用户控制器
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();
}
您的问题有三种解决方案。
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 进入 phpmyadmin 将你的 school_id,region_id 默认值设为 NULL。所以你很高兴去。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句