SQLSTATE [HY000]:常规错误:1364字段' user_id'没有默认值Laravel 5.8
SQLSTATE [HY000]:常规错误:1364字段'user_id'没有默认值(SQL:插入Reports
(detter> datery_name
>,ic
,<代码>电话,current_location
,status
,emg_code
,updated_at
,create_at
)值(DFD,女性,SDF,SDF,SDF,SDF,SDF,SDF,SDF,2022-06-13 05:53:11,2022-06-06-13 05:53 05:53 :11))
我可以知道如何解决这个问题吗? USER_ID是用户表的外键。
用户表:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('profile_image');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
用户模型:
public function reports()
{
return $this->hasMany(Report::class);
}
报告表:
Schema::create('reports', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('patient_name');
$table->string('gender');
$table->string('ic');
$table->string('phone');
$table->string('current_location');
$table->string('status');
$table->string('emg_code');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
ReportController创建Func。:ReportController
public function create()
{
$users = User::select('id', 'name')->get();
$users = $users->pluck('name');
return view('admin.reports.create', compact('users'));
}
Store Func。:HASUSER
public function store(Request $request)
{
$this->validate($request, [
'patient_name' => 'required|string',
'gender' => ['required', 'in:male,female'],
'ic' => 'required|string',
'phone' => 'required|string',
'current_location' => 'required|string',
'status' => 'required|string',
'emg_code' => 'required|string'
]);
$requestData = $request->all();
$report = Report::create($requestData);
foreach ($request->users as $user) {
$report->assignUser($user);
}
return redirect('admin/reports')->with('flash_message', 'Report added!');
}
特征:
public function user()
{
return $this->belongsTo(User::class);
}
public function assignUser($user)
{
return $this->users()->save(
User::whereName($user)->firstOrFail()
);
}
报告模型:
class Report extends Model
{
use HasUser;
protected $fillable = [
'user_id',
'patient_name',
'gender',
'ic',
'phone',
'current_location',
'status',
'emg_code'
];
}
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into reports
(patient_name
, gender
, ic
, phone
, current_location
, status
, emg_code
, updated_at
, created_at
) values (dfd, female, sdf, sdf, sdf, sdf, sdf, 2022-06-13 05:53:11, 2022-06-13 05:53:11))
May I know how can I solve this? The user_id is the foreign key of the Users Table.
Users Table:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('profile_image');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
User Model:
public function reports()
{
return $this->hasMany(Report::class);
}
Reports Table:
Schema::create('reports', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('patient_name');
$table->string('gender');
$table->string('ic');
$table->string('phone');
$table->string('current_location');
$table->string('status');
$table->string('emg_code');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
ReportController Create func.:
public function create()
{
$users = User::select('id', 'name')->get();
$users = $users->pluck('name');
return view('admin.reports.create', compact('users'));
}
ReportController Store func.:
public function store(Request $request)
{
$this->validate($request, [
'patient_name' => 'required|string',
'gender' => ['required', 'in:male,female'],
'ic' => 'required|string',
'phone' => 'required|string',
'current_location' => 'required|string',
'status' => 'required|string',
'emg_code' => 'required|string'
]);
$requestData = $request->all();
$report = Report::create($requestData);
foreach ($request->users as $user) {
$report->assignUser($user);
}
return redirect('admin/reports')->with('flash_message', 'Report added!');
}
HasUser Trait:
public function user()
{
return $this->belongsTo(User::class);
}
public function assignUser($user)
{
return $this->users()->save(
User::whereName($user)->firstOrFail()
);
}
Reports Model:
class Report extends Model
{
use HasUser;
protected $fillable = [
'user_id',
'patient_name',
'gender',
'ic',
'phone',
'current_location',
'status',
'emg_code'
];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论