Answer the question
In order to leave comments, you need to log in
How to combine 2 expressions in Eloquent into one - creating a model and getting its context along with each statement?
Good afternoon.
Thus, I create and get the context of the created model in $attribute$attribute = factory(Attribute::class)->create();
Next, I create communication models, everything is ok:
$attribute->multiples()->createMany(factory(Multiple::class, $multiples_count)->raw());
$attribute = factory(Attribute::class)->create()->each(function (Attribute $createdAttribute) {
$createdAttribute->multiples()->createMany(factory(Multiple::class, $multiples_count)->raw());
});
Answer the question
In order to leave comments, you need to log in
factory(Attribute::class)->create();
As far as I can understand, it will return an instance of the Attribute model. But then you chain.
A logical question arises each (each), sorry, what ? But even let's say that you have a typo, and you wanted to write
But then it's right
And in $attributes there will be a collection of Attribute instances.
Let's move on ... let's say we corrected this matter and wrote it correctly
$attributes = factory(Attribute::class, 2)->create()->each(function (Attribute $createdAttribute) {
$createdAttribute->multiples()->createMany(factory(Multiple::class, $multiples_count)->raw());
});
$multiples_count = 5;
$attributes = factory(Attribute::class, 2)->create()->each(function (Attribute $createdAttribute) use ($multiples_count) {
$createdAttribute->multiples()->createMany(factory(Multiple::class, $multiples_count)->raw());
});
$multiples_count = 5;
$attributes = factory(Attribute::class, 2)->create()->each(function (Attribute $createdAttribute) use ($multiples_count) {
$createdAttribute->multiples()->createMany(factory(Multiple::class, $multiples_count)->raw());
});
foreach ($attributes as $attribute) {
$attribute->doSomeThing(); // Вот вам и ваш "контекст"
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question