Axeia

->sync with additional values - Can't wrap my brain around it

Posted 4 months ago by Axeia

Hello,

I have a table like this

        Schema::create('trainer_shiny', function (Blueprint $table) {
            $table->string('trainer_name', 255);
            $table->string('pokemon_images_file_name', 255);
            //For pokémon that have a gender but without any visual difference.
            $table->tinyInteger('gender')->comment('1 = male, 2 = female, 3 = genderless'); 
            
            $table->primary(['trainer_name', 'pokemon_images_file_name', 'gender'], 'pk_trainer_shiny');
                
            $table->foreign('trainer_name')
                ->references('name')
                ->on('trainers')
                ->onDelete('CASCADE');
            $table->foreign('pokemon_images_file_name')
                ->references('file_name')
                ->on('pokemon_images')
                ->onDelete('CASCADE');
        })

And I want to insert values using the sync method on the trainer (e.g. $trainer->shinyPokemons()->sync($values); ) But I'm not quite getting how to insert the values.

        $trainer->shinyPokemons()->sync(['pokemon_icon_001_00_shiny.png' => [
            'gender' => [1, 2]
        ]]);

Is getting me: Error: "Array to string conversion (SQL: insert into trainer_shiny (gender, pokemon_images_file_name, trainer_name) values (1, pokemon_icon_001_00_shiny.png, Axxeia)) ◀"

        $trainer->shinyPokemons()->sync(['pokemon_icon_001_00_shiny.png' => [
            ['gender' => 1], 
            ['gender' => 2]
        ]]);

is getting me: "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into trainer_shiny (pokemon_images_file_name, trainer_name, 0, 1) values (pokemon_icon_001_00_shiny.png, Axxeia, 1, 2)) ◀"

Am I misunderstanding and does it only work for columns that aren't part of the primary key like my gender field is? Is there any way to make it work?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.