【问题标题】:Optimize Chunk in Laravel在 Laravel 中优化块
【发布时间】:2020-04-16 16:42:32
【问题描述】:

我的 if() 条件中的这个子查询卡住了我的代码。 任何人都有优化这个的想法?

public function handle()
  {
    print "???? Searching person \n";

    DB::table('excercise_pessoa')->orderBy('id')->chunk(1000, function ($people) {
      foreach ($people as $person) {
        $excercise = Excercise::find($person->excercise_id);
        $course = $excercise->grade->course;
        $personReal = Pessoa::find($person->pessoa_id);

        if ($course->pessoas->where('id', '=', $person->pessoa_id)->count() == 0) {
          $course->pessoas()->attach($personReal);

          print "???? Associating course to people ID: $person->pessoa_id \n";
        }
      }
    });

    print "???? Finished migration! \n";
  }

【问题讨论】:

  • 使用关系manyToMany进行优化
  • @A.ANoman 怎么样?我需要验证课程是否没有为人们重复

标签: php laravel eloquent orm


【解决方案1】:

我这样解决,

删除 if() 条件并使用:

$course->pessoas()->syncWithoutDetaching($personReal);

【讨论】:

    猜你喜欢
    • 2018-01-10
    • 2014-12-28
    • 1970-01-01
    • 2017-01-08
    • 2020-12-05
    • 2018-05-09
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多