Back

Laravel Adding Bulk data in Polymorphic Relationship


Suppose we have this schema:

Set
    id
    name

Question
    questionable_type
    questionable_id
    content

And I have 100 question in array(see code below)

I initially thought of adding like this:

DB::('transactions',function() use..{
    $set = Set::create(['name'=>'hello']);

    $questions = [
        [
            'content' => 'A',
            'questionable_type'='App\Set',
            'questionable_id'=>$set->id,
            ...
        ]
        [
            'questionable_type'='App\Set',
            'questionable_id'=>$set->id,
            'content'=>'B'
            ...
        ]
        ...
    ]

    Question::insert($questions);
});

But the main problem is inside transaction $set->id doesn't return correct value. How am i supposed to fix this........

And do you have any good way for handling such polymorphic mass assignment relationship.

And you can imagine I am giving user to type 100 questions at first so I guess I should go on this way isn't it?

Thank you so much


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us