能否使用ORM在一次操作中创建多行,并具有不同值的属性?

huangapple go评论62阅读模式
英文:

Is it possible to create multiple rows in a single pass with an attribute with different values using the ORM?

问题

抱歉,我只提供代码的翻译,不回答翻译问题。以下是您提供的代码的翻译:

对于我问题的标题,我很抱歉,如果你能帮助我改进它,我将非常感激。

现在的问题是:我有一个应用程序,我的用户对比赛进行一些预测,就像体育投票一样。当我评分他们的预测时,由于我们的规则,我会执行几项操作。我首先要做的是为那些没有做出预测的用户分配一个非常不太可能的预测。我这样做是为了在他们忘记下注的情况下,给他们一些赢的机会。

所以,我首先要做的是获取所有已经预测的用户。

$pronos = Pronostico::where('juego_id', $juego->id)
    ->pluck('user_id');

然后,我使用这些信息来获取所有没有预测的用户...

$users = User::whereNotIn('id', $pronos)->get();

最后,我为所有这些用户创建一个预测(Pronostico 在西班牙语中),如下所示:

foreach ($users as $user) {
    Pronostico::create([
        'user_id' => $user->id,
        'prediction' => -4,
        // 其他属性
    ]);
}

是否有一种方法可以省去foreach循环?比如通过将用户传递给数组来插入多行数据或类似的方法?以使代码更加简洁?

英文:

I'm sorry for the question's title, if you can help me to make it better I'd more than thankful.

Now the question: I have an app where my users do some predictions on games, like a sport poll. When I score they predictions I do several things because of the rules we have. The first thing I do is to assign a very unlikely prediction to the users that didn't make a prediction. I do this in order to give them some chance to win if they forgot to place their predictions.

So, first thing I do is to get all the users that did predict.

  $pronos = Pronostico::where('juego_id', $juego->id)
    ->pluck('user_id');

Then I use this info to get all the users that didn't predict...

  $users = User::whereNotIn('id', $pronos)->get();

And finally I create a prediction (Pronostico in spanish) for all these users:

  foreach ($users as $user) {
    Pronostico::create([
        'user_id' => $user->id,
        'prediction' => -4,
        // other attributes
    ]);
  }

Is there a way to take out that foreach? Like inserting several rows by passing the users in an array or something like that? something to make this cleaner?

答案1

得分: 1

你可以使用关联来直接获取与前两个查询相同的结果并对它们执行操作。尝试一下,看看是否符合你的预期。

User::doesntHave('pronosticos', function (Builder $query) use ($juego) {
    return $query->where('juego_id', $juego->id);
})
    ->pronosticos()
    ->create([
        'prediction' => -4,
    ]);

因此,你只想从所有的Users中筛选出那些没有pronosticos(这部分是->doesntHave('pronosticos'),必须匹配UserPronostico模型之间的关联名称),然后进入关联(因为你想为它们创建pronosticos),并创建prediction = -4的记录。

这应该会按照你的预期工作。有关doesntHave的更多信息,请参阅文档。

英文:

You can use relationships, so directly get the same as the first 2 queries and do something with them. Try it out and see if it works for you as expected.

User::doesntHave('pronosticos', function (Builder $query) use ($juego) {
    return $query->where('juego_id', $juego->id);
})
    ->pronosticos()
    ->create([
        'prediction' => -4,
    ]);

So, from all Users you only want those that do not have pronosticos (this part would be ->doesntHave('pronosticos') and must match the relationship name between User and Pronostico models), then you go to the relationship (because you want to create pronosticos for them), and create with prediction = -4.

This should work as expected for you. More about doesntHave on the docs.

huangapple
  • 本文由 发表于 2023年6月2日 07:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定