Problems when trying to make a relationship on Laravel

Viewed 93

I have a problem when I try to make a migration. I have an event table and a color table. I have already tried to make a relationship between those tables using Tinker and there was no problem but when I used PhpmyAdmin to click on the id (to see what field it is), I just couldn't because there's no link between those tables. Here is my two migrations :

First migration :

//Event migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class EventList extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('events', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nom', 45);
            $table->integer('couleur_id')->unsigned()->index();
            $table->integer('annee_cours_id')->unsigned()->index();
        });
        Schema::enableForeignKeyConstraints('events', function (Blueprint $table){
            $table->foreign('couleur_id')->references('id')->on('couleurs');
            $table->foreign('annee_cours_id')->references('id')->on('annee_cours');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('events');
    }
}

Second migration :

//Color migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class Couleur extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('couleurs', function (Blueprint $table){
           $table->increments('id');
           $table->string('nom', 45);
           $table->string('code_hexa', 45);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('couleurs');
    }
}

I'm actually using the PhpmyAdmin conceptor (just like on the picture below) to make relationships more easily but I would like to do the same operation using only code. Is there a solution to do this easily on Laravel without using the PhpmyAdmin conceptor ?

Thank you in advance for your answers.

Picture of a relationship example on PhpmyAdmin

EDIT : After using php artisan migrate --pretend, I got some lines describing what the migration did precisely. There's the output :

QuadrimestreList: create table `quadrimestres` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasTypeEvent: create table `event_has_type_events` (`Event_id` int unsigned not null, `type_event_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasTypeEvent: alter table `event_has_type_events` add index `event_has_type_events_event_id_index`(`Event_id`)
EventHasTypeEvent: alter table `event_has_type_events` add index `event_has_type_events_type_event_id_index`(`type_event_id`)
EventHasTypeEvent: SET FOREIGN_KEY_CHECKS=1;
EventList: create table `events` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null, `couleur_id` int unsigned not null, `annee_cours_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventList: alter table `events` add index `events_couleur_id_index`(`couleur_id`)
EventList: alter table `events` add index `events_annee_cours_id_index`(`annee_cours_id`)
EventList: SET FOREIGN_KEY_CHECKS=1;
EventHasQuadrimestre: create table `event_has_quadrimestres` (`Event_id` int unsigned not null, `quadrimestre_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasQuadrimestre: alter table `event_has_quadrimestres` add index `event_has_quadrimestres_event_id_index`(`Event_id`)
EventHasQuadrimestre: alter table `event_has_quadrimestres` add index `event_has_quadrimestres_quadrimestre_id_index`(`quadrimestre_id`)
EventHasQuadrimestre: SET FOREIGN_KEY_CHECKS=1;
EventHasLocal: create table `event_has_locals` (`Event_id` int unsigned not null, `local_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasLocal: alter table `event_has_locals` add index `event_has_locals_event_id_index`(`Event_id`)
EventHasLocal: alter table `event_has_locals` add index `event_has_locals_local_id_index`(`local_id`)
EventHasLocal: SET FOREIGN_KEY_CHECKS=1;
EventHasEnseignant: create table `event_has_enseignants` (`Event_id` int unsigned not null, `enseignant_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasEnseignant: alter table `event_has_enseignants` add index `event_has_enseignants_event_id_index`(`Event_id`)
EventHasEnseignant: alter table `event_has_enseignants` add index `event_has_enseignants_enseignant_id_index`(`enseignant_id`)
EventHasEnseignant: SET FOREIGN_KEY_CHECKS=1;
Enseignant: create table `enseignants` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Couleur: create table `couleurs` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null, `code_hexa` varchar(45) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
AnneeCours: create table `annee_cours` (`id` int unsigned not null auto_increment primary key, `nom` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Horaire: create table `horaires` (`id` int unsigned not null auto_increment primary key, `nom` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Local: create table `locals` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null, `commentaire` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Reservation: create table `reservations` (`id` int unsigned not null auto_increment primary key, `numero_semaine` int not null, `date` datetime not null, `heure_debut` varchar(45) not null, `heure_fin` varchar(45) not null, `Event_id` int unsigned not null, `horaire_id` int unsigned not null, `local_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Reservation: alter table `reservations` add index `reservations_event_id_index`(`Event_id`)
Reservation: alter table `reservations` add index `reservations_horaire_id_index`(`horaire_id`)
Reservation: alter table `reservations` add index `reservations_local_id_index`(`local_id`)
Reservation: SET FOREIGN_KEY_CHECKS=1;
TypeEvent: create table `type_events` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'

Don't worry about the other tables, they have the same problem than the 'Event' and the 'Couleurs' table. I think that it's a problem about foreign keys but I don't see precisely where the problem is. Is the output helpful ?

EDIT 2 : OK, it looks better but when I take a look in the PhpmyAdmin conceptor, there's no relationship between the tables. I don't know if that's normal, maybe PhpmyAdmin doesn't recognize relationships the same way than Laravel migrations does ? Sorry if I'm asking so much help.

1 Answers

use “Schema::table(...)“, and NOT Schema::enableForeignKeyConstraints

it should work...

Related