samedi 22 décembre 2018

Defining Relationships in Laravel

I'm new to understanding relationships and understanding the MVC. It really helps if you could guide me why I'm receiving this error, and how I could solve this in the future.

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`guest_log`.`logs`, CONSTRAINT `fk_logs_Dept1` FOREIGN KEY (`department_to_visit`) REFERENCES `departments` (`dept_id`) ON DELETE NO ACTION ON UPDATE NO ACTION) (SQL: insert into `logs` (`guest_id`, `guest_id_number`, `purpose`, `department_to_visit`, `date_time_in`, `updated_at`, `created_at`) values (2, 123, abc, 0, 2018-12-22 10:29:29, 2018-12-22 10:29:29, 2018-12-22 10:29:29))

Here is the SQL for department, logs and guest

-- ----------------------------
-- 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;


-- ----------------------------
-- 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 Departments
-- ----------------------------
DROP TABLE IF EXISTS 'Departments'
CREATE TABLE `Deptartments` (
  `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`)
) 

Here is the model for Logs, department and Guest.

class Log extends Model
{
    protected $fillable = ['guest_id','guest_id_number','date_time_in','date_time_out','purpose','department_to_visit'];

}

class Department extends Model
{
    protected $fillable = ['dept_id','dept_code', 'dept_description', 'dept_assign'];


}
class Guest extends Model
{
    protected $fillable = ['first_nane',
                            'middle_name',
                            'last_name',
                            'gender',
                            'email',
                            'tel_no',
                            'mobile_no',
                            'company',
                            'photo'
        ];
}

And this is the part that gets the error when form is submitted

 {!! BootForm::inline(['route'=>['logs.store'],'method'=>'POST']) !!}

        {!! BootForm::select('guest_id', 'Guest Name',\App\Guest::pluck('full_name','id')->toArray() ,null, [])!!}


        {!! BootForm::tel('guest_id_number','ID Number',null, ['required']) !!}
        {!! BootForm::select('department_to_visit','Department to Visit', \App\Department::pluck('department_to_visit','dept_id'),null,[]) !!}
        {!! BootForm::text('purpose',null,null,['required']) !!}

        {!! BootForm::submit('Save') !!}

        {!! BootForm::close() !!}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2SaDhRI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire