Laravel添加新条目时,将当前用户的ID添加到表中
是的,所以我有一个带有用户ID和另一个避难所的用户表,用户将在表中添加避难所,我想向该人的用户ID展示,这些人在我使用的$ table-> foreferid('id') - >co点('id') - > on('用户');那给了我 SQLSTATE [HY000]:常规错误:1364字段'ID'没有默认值
避难所表
public function up()
{
Schema::create('animal_shelters', function (Blueprint $table) {
$table->id('shelter_id');
$table->timestamps();
$table->foreignId('id')->references('id')->on('users');
$table->string('shelter_name');
$table->string('shelter_address');
$table->string('shelter_mobile_number');
$table->string('shelter_type');
});
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
$table->timestamps();
});
}
错误:
INLUMINES \ DATABASE \ QUERYEXECTION
SQLSTATE [HY000]:常规错误:1364字段'ID'没有默认值
插入进入数据库
public function store(Request $request)
{
$Animal_shelter = new Animalshelter();
$Animal_shelter->shelter_name = $request->input('shelter_name');
$Animal_shelter->shelter_address = $request->input('shelter_address');
$Animal_shelter->shelter_mobile_number = $request->input('shelter_mobile_number');
$Animal_shelter->shelter_type = $request->input('shelter_type');
$Animal_shelter->save();
return redirect()->back()->with('status', 'shelter added successfully');
}
yeah so I have a user table with user id and another table of shelters, users will add shelters to the table and I want to show the user id of the person that adds that particular shelter to the table i am using $table->foreignId('id')->references('id')->on('users'); that gives me
SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value
Shelter Table
public function up()
{
Schema::create('animal_shelters', function (Blueprint $table) {
$table->id('shelter_id');
$table->timestamps();
$table->foreignId('id')->references('id')->on('users');
$table->string('shelter_name');
$table->string('shelter_address');
$table->string('shelter_mobile_number');
$table->string('shelter_type');
});
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
$table->timestamps();
});
}
Error :
Illuminate \ Database \ QueryException
SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value
inserting into database
public function store(Request $request)
{
$Animal_shelter = new Animalshelter();
$Animal_shelter->shelter_name = $request->input('shelter_name');
$Animal_shelter->shelter_address = $request->input('shelter_address');
$Animal_shelter->shelter_mobile_number = $request->input('shelter_mobile_number');
$Animal_shelter->shelter_type = $request->input('shelter_type');
$Animal_shelter->save();
return redirect()->back()->with('status', 'shelter added successfully');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这仅仅是因为您未设置
user_id
字段。只需在save()
之前将其添加到您的中
对象 ,您可以链函数,类似
第二个解决方案
第三个解决方案
It's just because you not set the
user_id
field. simply add it to your object beforesave()
will fix the problemOr just change your shelter migration file so the column accept null value
Or last solution, if you set up relationship between User and Shelter on eloquent level, you can chain function, something like this
Second solution
Third solution
不,它不会自动添加它。当您添加到庇护所表中时,您必须将外国ID设置为正在创建它的用户的用户ID。
No, it won't add it automatically. When you add to the shelter table you must set the foreignid to the user id of the user that is creating it.