我仍然不了解关系和MVC。但是我尝试遵循我在网上阅读的所有内容,但仍然收到此错误。
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`guest_log`.`logs`, CONSTRAINT `fk_logs_guests1` FOREIGN KEY (`guest_id`) REFERENCES `guests` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
这是用于访客,日志和部门的SQL
DROP TABLE IF EXISTS `Dept`;
CREATE TABLE `Dept` (
`dept_id` int(5) NOT NULL AUTO_INCREMENT,
`dept_code` varchar(30) CHARACTER SET latin1 DEFAULT NULL,
`dept_description` varchar(75) CHARACTER SET latin1 NOT NULL,
`dept_assign` enum('Academic','Admin') CHARACTER SET latin1 NOT NULL DEFAULT 'Academic',
`is_deleted` enum('Yes','No') CHARACTER SET latin1 DEFAULT 'No',
PRIMARY KEY (`dept_id`)
)
-- ----------------------------
-- Table structure for guests
-- ----------------------------
DROP TABLE IF EXISTS `guests`;
CREATE TABLE `guests` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`middle_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`last_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`gender` varchar(6) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`tel_no` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`mobile_no` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`company` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`photo` varchar(500) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ----------------------------
-- Table structure for logs
-- ----------------------------
DROP TABLE IF EXISTS `logs`;
CREATE TABLE `logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guest_id` int(11) NOT NULL,
`guest_id_number` datetime NOT NULL,
`date_time_in` datetime NOT NULL,
`date_time_out` datetime DEFAULT NULL,
`purpose` varchar(45) COLLATE utf8mb4_unicode_ci NOT NULL,
`department_to_visit` int(11) DEFAULT NULL,
PRIMARY KEY (`id`,`guest_id`),
KEY `fk_logs_guests1_idx` (`guest_id`),
KEY `fk_logs_Dept1_idx` (`department_to_visit`),
CONSTRAINT `fk_logs_guests1` FOREIGN KEY (`guest_id`) REFERENCES `guests` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_logs_Dept1` FOREIGN KEY (`department_to_visit`) REFERENCES `Dept` (`dept_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
这里是模型。
class Guest extends Model
{
protected $table = 'guests';
public function logs()
{
return $this->hasMany('App\Log');
}
}
class Log extends Model
{
protected $table = 'logs';
public function guests()
{
return $this->belongsTo('App\Guest','guest_id','id');
}
public function departments()
{
return $this->belongsTo('App\Department','department_to_visit','dept_id');
}
}
class Department extends Model
{
protected $table = 'dept';
public function logs()
{
return $this->hasMany('App\Log','department_to_visit','dept_id');
}
}
每当我尝试为Log创建一个新条目时,都会收到此Integrity Constraint Violation错误。请帮助我解决并了解为什么我会遇到此问题。
更新:
创建日志条目的代码
<div class="container-fluid">
{!! BootForm::inline(['route'=>['logs.store'],'method'=>'POST']) !!}
{!! BootForm::select('guest_id', 'Guest Name',\App\Guest::pluck('last_name','id'),null, [])!!}
{!! BootForm::tel('guest_id_number','ID Number',null, ['required']) !!}
{!! BootForm::select('department_to_visit','Department to Visit', App\Department::pluck('dept_description','dept_id'),null,[]) !!}
{!! BootForm::text('purpose',null,null,['required']) !!}
{!! BootForm::submit('Save') !!}
{!! BootForm::close() !!}
</div>
log.store方法
public function store(Request $request)
{
$this->validate($request, [
'guest_id'=>'required',
'guest_id_number'=>'required',
'purpose'=>'required',
'department_to_visit']);
$logs = new Log();
$logs->guest_id = $request->guest_id;
$logs->guest_id_number = $request->guest_id_number;
$logs->purpose = $request->purpose;
$logs->department_to_visit = $request->department_to_visit;
$logs->date_time_in = Carbon::now();
$logs->save();
return redirect(route('home'));
}
答案 0 :(得分:0)
可能是您没有传递和现有访客ID。 为了正常检查,我搜索了模型,例如。
$guest = Guest::findOrFail($request->guest_id);
并使用
$logs->guest_id = $guest->id;
尝试一下,让我知道它是如何工作的:)