Recent Topics

Using ProductRepository to seed products



  • Hello,
    I'm trying to build a custom seeder that seeds configurable products, however I can't get it to work when I'm adding the "super_attributes" array inside the $data needed by the repository.

    $attributes = [
                'type' => 'configurable',
                'attribute_family_id' => '3',
                'sku' => $card->CA_COMPTEUR,
                'super_attributes' => array(
                    'color' => array(
                        0 => 1,
                        1 => 2,
                        2 => 3,
                    ),
                    'print_support' => array(
                        0 => 11,
                        1 => 12,
                    ),
                    'print_size' => array(
                        0 => 13,
                        1 => 14,
                        2 => 15,
                        3 => 16,
                    )
                ),
                'family' => '3'
            ];
    
    
            $product = app(ProductRepository::class)->create($attributes);
    

    I'm getting an array to string conversion error. I don't understand because when I'm duping the data sent from the backoffice this is the exact same array. What am I doing wrong ?
    When I'm deleting the "super_attributes" array from the data this work perfectly but then I can't have the variants.

    Have a nice day !



  • Hi @gciliberti,

    Can you share the full error or screenshot because the array looks fine to me?



  • @gciliberti said in Using ProductRepository to seed products:
    d71ef68f-8c25-40ee-814e-47baa478e1d5-image.png

    I get this error when i come to create() function of the Configurable type, to me the array look fine, I'm sending exactly the same thing as the backoffice. Maybe calling the repository from a seeder is the issue ?

            $product = $this->productRepository->getModel()->create($data);
    
    

    ^
    The bug is produced by this line in the configurable type.

    I'm stuck on this issue for several hours and I can't find a solution, I'm desperate.

    Can i get around this by using the product factory ? Will it create the variants for me like the repository can do ?

    Thank you a lot !



  • I found the solution !
    Laravel allow us to bypass $fillable property from the model when using a seeder, in my case I didn't want it.
    I simply had to use

    Model::reguard();
    

    and it worked !


Log in to reply