如何在Laravel中基于嵌套数组插入关系?

时间:2023-01-26 14:33:25

I have 3 tables tests, questions, options.

我有3个表测试,问题,选项。

As you can imagine

你可以想象

  1. a test has many questions
  2. 测试有很多问题
  3. a question belongs to a test
  4. 一个问题属于测试
  5. a question has many options
  6. 一个问题有很多选择
  7. an option belongs to a question
  8. 选项属于问题

These relations are set up in the models already.

这些关系已经在模型中建立。

I got the data from the front end in this form:

我以这种形式从前端获取数据:

array:3 [
  "name" => "First Test"
  "preparation" => "First Test prep"
  "questions" => array:2 [
    0 => array:2 [
      "title" => "Some question"
      "options" => array:4 [
        0 => "a"
        1 => "b"
        2 => "c"
        3 => "d"
      ]
    ]
    1 => array:2 [
      "title" => "Another question"
      "options" => array:4 [
        0 => "e"
        1 => "f"
        2 => "g"
        3 => "h"
      ]
    ]
  ]
]

This data perfectly represents these relationships. In fact if I were using a NoSql database I would simply store this in the database.

这些数据完美地代表了这些关系事实上,如果我使用NoSql数据库,我只会将其存储在数据库中。

My question is "what is the best way to store all of this data at once while using eloquent in Laravel"?

我的问题是“在Laravel中使用雄辩的同时,一次存储所有这些数据的最佳方法是什么?”

Note: It is in the form of Laravel's collection.

注意:它是Laravel系列的形式。

2 个解决方案

#1


1  

 class Test extends Model {

    $table = 'tests';
    function questions(){
                return $this->hasMany(Question::class, 'test_id');
            }

 }

class Question extends Model {

    $table = 'questions';
    function answers(){
        return $this->hasMany(Answer::class, 'question_id');
            }

    function test(){
        return $this->belongsTo(Test::class, 'test_id');
    }

}

class Answer extends Model {

    $table = 'answers';

    function question(){
        return $this->belongsTo(Question::class, 'question_id');
    }

}

of course i showed only relations ,tables and foreign keys. you can add any additional data to your models.

当然,我只展示了关系,表格和外键。您可以向模型添加任何其他数据。

#2


0  

You have to create a structure of model objects from given data. JMS Serializer is a great library to do that, and it has laravel integration.

您必须从给定数据创建模型对象的结构。 JMS Serializer是一个很棒的库,它具有laravel集成功能。

#1


1  

 class Test extends Model {

    $table = 'tests';
    function questions(){
                return $this->hasMany(Question::class, 'test_id');
            }

 }

class Question extends Model {

    $table = 'questions';
    function answers(){
        return $this->hasMany(Answer::class, 'question_id');
            }

    function test(){
        return $this->belongsTo(Test::class, 'test_id');
    }

}

class Answer extends Model {

    $table = 'answers';

    function question(){
        return $this->belongsTo(Question::class, 'question_id');
    }

}

of course i showed only relations ,tables and foreign keys. you can add any additional data to your models.

当然,我只展示了关系,表格和外键。您可以向模型添加任何其他数据。

#2


0  

You have to create a structure of model objects from given data. JMS Serializer is a great library to do that, and it has laravel integration.

您必须从给定数据创建模型对象的结构。 JMS Serializer是一个很棒的库,它具有laravel集成功能。