Search code examples
laravelvue.jsaxioslaravel-api

How to store item with one-to-many relations into sql-table?


I have any form created on vuetify.js

In this for I have field:

  1. Normal input field "Name", will this be stored in the table "person" (model Person, structure: id, name, ...).
  2. Multiple select "Languages", should this be stored in the table "persons_languages" (structure: id, person_id, language_id).

1 person can speak more than 1-2-3 languages.

<v-form ref="form" @submit.prevent="addPerson">

<v-row>

    <v-col
        cols="12"
        md="6">
        <v-text-field
            v-model="person.name"
            label="Name">
        </v-text-field>
    </v-col>

    <v-col
        cols="12"
        md="6">
        <v-autocomplete
            chips
            deletable-chips
            multiple
            v-model="???"
            :items="allLanguages"
            :item-text="item =>`${item.name} (${item.local_name})`"
            item-value="id"
            label="Spoken languages">
        </v-autocomplete>
    </v-col>

</v-row>

</v-form>

<script>

export default {

    data() {
        return {
            person: {},

            allLanguages: [
                {
                    "id": 1,
                    "name": "Afar",
                    "code": "aa"
                },
                {
                    "id": 2,
                    "name": "Abkhazian",
                    "code": "ab"
                },
                {
                    "id": 3,
                    "name": "Afrikaans",
                    "code": "af"
                }
            ]
        }
    },

    methods: {
        addPerson() {
            this.axios
                .post('/api/persons', this.person)
                .then(response => {
                    this.$router.push({name: 'indexPersons'});
                })
                .catch(error => console.log(error))
        },
    },
}

</script>

How to store array of selected languages into "persons_languages" with ID of new not yet created person?

Tnx.


Solution

  • One of the way to save one to many relation data is using foreach loop. let you create a person which have 2 languages.

    $person= Person::create([
        'name' => 'SOmthing',
    ]);
    

    Person select 02 languages. $languauges variable have multiple data like:

    $languages=[
                    {
                        "id": 1,
                        "name": "Afar",
                        "code": "aa"
                    },
                    {
                        "id": 3,
                        "name": "Afrikaans",
                        "code": "af"
                    }
                ]
    

    Use foreach loop to save languages data in person_languages table against person Id create above.

    foreach($languages as $lang) {
           PersonLanguage::create([
               'person_id' => $person->id,
                'language_id' => $lang->id
          ]);
    }
    
    

    You may use the create method, which accepts an array of attributes, creates a model, and inserts it into the database. The save/create method will automatically add the appropriate person_id value to the new Personlanguages model.

    Read Documentation to more about Save Many and Create Many Save/ Create Many For example one post have many comments.

    $post = App\Post::find(1);
     
    $post->comments()->createMany([
        [
            'message' => 'A new comment.',
        ],
        [
            'message' => 'Another new comment.',
        ],
    ]);