swenneveu's avatar

Ternary relation

Hello everyone, On a laravel project i need to use a ternary relationship so i have 3 table The table Questionnaire


 Schema::create('questionnaires', function (Blueprint $table) {
            $table->id('id');
            $table->string('Quanti');
            $table->date('date_heure');
            $table->timestamps();
        });

The table reponses

Schema::create('reponses', function (Blueprint $table) {
            $table->id('id');
            $table->string('libelle_reponse', 50);
            $table->integer('ordre');
            $table->timestamps();
        });

The table Questions


Schema::create('questions', function (Blueprint $table) {
            $table->id('id');
            $table->string('libelle_question', 50);
            $table->integer('ordre');
            $table->string('graphique', 50);
            $table->foreignId('type_id')->constrained('type', 'id');
            $table->timestamps();
        });

And finaly i have the pivot table


 Schema::create('questionnaire_reponse_question', function (Blueprint $table) {
            $table->foreignId('reponse_id')->constrained('reponses', 'id');;
            $table->foreignId('question_id')->constrained('questions', 'id');;
            $table->foreignId('questionnaire_id')->constrained('questionnaires', 'id');;
            $table->primary(['reponse_id', 'questionnaire_id', 'question_id']);
            $table->timestamps();
        });

But now i need to create the relationships between my different models by using the pivot table, i've find a solution who actually work so it's to create an other model as a pivot model, but i want to know if a all made solution exist actually. Here is my mcd if it can help you better understand my problem prnt.sc /HpW8pQpyAO6V

Thanking you in advance for your answers ;)

0 likes
2 replies
martinbean's avatar

@swenneveu I don’t really understand what is it you’re trying to do? What is the relation you’re trying to define, and why do you think it needs to be a “ternary”?

If you’re building a questionnaire then you need three models:

  • Questionnaire
  • Question
  • Answer

A questionnaire has many questions. A question has many answers. That’s it.

In your answers table, put a unique constraint on the combination of user_id and question_id and then a user will only be able to answer each question once.

1 like
alden8's avatar

In Laravel, you can define many-to-many relationships using the belongsToMany method in your models. However, Laravel doesn't natively support a direct three-way (ternary) relationship.

You can achieve this by defining two separate many-to-many relationships. Here's how you can define these relationships:

In your Questionnaire model:

` public function questions() { return $this->belongsToMany(Question::class, 'questionnaire_reponse_question'); }

public function reponses() { return $this->belongsToMany(Reponse::class, 'questionnaire_reponse_question'); } `

In your Question model:

` public function questionnaires() { return $this->belongsToMany(Questionnaire::class, 'questionnaire_reponse_question'); }

public function reponses() { return $this->belongsToMany(Reponse::class, 'questionnaire_reponse_question'); } `

In your Reponse model:

` public function questionnaires() { return $this->belongsToMany(Questionnaire::class, 'questionnaire_reponse_question'); }

public function questions() { return $this->belongsToMany(Question::class, 'questionnaire_reponse_question'); } `

This way, you can access the related models from any of the three models. For example, to get all responses for a specific questionnaire, you can do $questionnaire->reponses.

However, this doesn't directly represent a ternary relationship, as it doesn't allow you to directly link a questionnaire, a question, and a response together in one operation. If you need to do this, you might need to create a custom pivot model and use it to manually manage the relationships.

1 like

Please or to participate in this conversation.