dimanche 23 décembre 2018

Foreign Key Constraint Fail in laravel

I'm trying to create log entries which has foreign keys of guest_id and department_to_visit

SQL:

DROP TABLE IF EXISTS `logs`;
CREATE TABLE `logs`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `guest_id` int(11) NOT NULL,
  `guest_id_number` int(11) NOT NULL,
  `date_time_in` datetime(0) NOT NULL,
  `date_time_out` datetime(0) NULL DEFAULT NULL,
  `purpose` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `department_to_visit` int(11) NULL DEFAULT NULL,
  `updated_at` timestamp(6) NULL DEFAULT NULL,
  `created_at` timestamp(6) NULL DEFAULT NULL,
  PRIMARY KEY (`id`, `guest_id`) USING BTREE,
  INDEX `fk_logs_guests1_idx`(`guest_id`) USING BTREE,
  INDEX `fk_logs_dept1_idx`(`department_to_visit`) USING BTREE,
  CONSTRAINT `fk_logs_dept1` FOREIGN KEY (`department_to_visit`) REFERENCES `dept` (`dept_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_logs_guests1` FOREIGN KEY (`guest_id`) REFERENCES `guests` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)

Logs Model

class Logs 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');
    }

I keep receiving the error 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 dept (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 (4, 2, 2, 1, 2018-12-23 12:53:22, 2018-12-23 12:53:22, 2018-12-23 12:53:22))

whenever I try to create new log record using

LogController

  public function store(Request $request)
    {
        $this->validate($request, [
            'guest_id'=>'required',
            'guest_id_number'=>'required',
            'purpose'=>'required',
            'department_to_visit']);
        $logs = new Logs();
        $guest = Guest::findOrFail($request->guest_id);
        $logs->guest_id = $guest->id;
        $logs->guest_id_number = $request->guest_id_number;
        $logs->purpose = $request->purpose;
        echo $request->department_to_visit;
        $department= Department::findOrFail($request->department_to_visit);
//        echo '<pre>',var_dump($department),'</pre>';
        $logs->department_to_visit =$department->dept_id;
        $logs->date_time_in = Carbon::now();
        $logs->save();
//        return redirect(route('home'));
    }

using this method

 <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_code','dept_id'),null,[]) !!}
        {!! BootForm::text('purpose',null,null,['required']) !!}

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

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


    </div>

I used to receive errors on guest_id but resolved it. I thought it won't be different with guest_id but department_to_visit continues to receive errors. Please help me out in solving and understanding the cause of the problem



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire