【问题标题】:Get rows by column in Laravel Many to Many relationship在 Laravel 多对多关系中按列获取行
【发布时间】:2020-05-12 20:10:50
【问题描述】:

Laravel 多对多关系

您好,我一直在尝试按周从数据库中获取数据。

我有下一张表(多对多关系)

  1. 例程:id、plan_id、user_id。
  2. 练习:ID、姓名、描述、图片。
  3. exercise_routine:id、routine_id、exercise_id、周、日。

关系

例行公事

public function exercises() {
    return $this->belongsToMany(Exercise::class)->withPivot('week', 'day', 'completed')->orderBy('week','asc');;
}

锻炼

public function routines() {
    return $this->belongsToMany(Routine::class)->withPivot('week', 'day', 'completed');
}

我想像这样按周获取所有行。

    Json example:
    {
      "week": [
        1: {
         "exercises": [
           {
             "name": "Abs"
           }
         ]
        },
        2: {
         ...
        }
      ]

    }

我已经试过了

 if(!empty($routine)) {

            foreach ($routine->exercises as $exercise) {
                $week = $exercise->pivot->week;

                if($week == $previous_week) {
                    array_push($this->weeks, $exercise->pivot->week);
                } else {
                    array_push($this->weeks, [
                        $exercise->pivot->week => $exercise->pivot->week
                    ]);
                }

                $previous_week = $exercise->pivot->week;

            }

//            dd($this->weeks);

//            return DB::table('exercise_routine')->where('routine_id',$routine->id)->max('week');
        }

解释

exercise_routine 表有一个周数来按周分隔练习。 我需要创建一个数组,如果周为 1,则推入数组 1,如果下一个为 1,则推入同一个数组。如果下一个是 2 或不同,只需按数字 2。

我不确定你是否能理解我的意思,它只是想找到实现它的方法。

非常感谢。

【问题讨论】:

  • 您需要制作模型并定义关系。通过这种方式,您将能够获得所需格式的数据。意味着它将自动为您定义级别。
  • 你的意思是,我在多对多关系中定义了一个模型?练习例程?
  • 是的,如果你在 exerice_routine 模型中定义了一个关系,它既属于例程,也属于运动模型。因此,您将查询第一个模型哪个 exercise_routine 并且您将急切地加载其他两个关系。所以它会给你一个层次结构。
  • 是的,但这并不能解决我的问题,它即将获取数据并推送数组,因此与关系无关。
  • 好吧,我在一个项目中做了这件事,但是查询构建器加入了 laravel。我的情况是这样的,我有产品、购买、return_products、return_purchases,所有这些都必须在 created_at 日期顺序中,我认为有 10 或 12 个表加入。如果这适合您,请告诉我。

标签: laravel eloquent


【解决方案1】:

使用收集方法mapToGroups 可以帮助您简化对exercises 的分组。

以下是我将如何将其用于您的案例的示例:

if(!empty($routine)){
  // creating groups of exercise by week [1 => ['abs'], 2 => []]
  $exerciseWeeks = $routine->exercises->mapToGroups(function($exercise, $k){
      return [$exercise->pivot->weeks => $exercise->name];
    })
    ->map(function($exerciseInWeek, $k){ // creating your json format from here point on
      $weekObj = new \stdClass;
      $weekObj->exercises = [];
      foreach($exerciseInWeek as $exerciseName){
        $exerciseObj = new \stdClass;
        $exerciseObj->name = $exerciseName;
        $weekObj->exercises[] = $exerciseObj;
      }
      return $weekObj;
    });

  // creates the final json object
  $routineObj = new \stdClass;
  $routineObj->week = $exerciseWeeks->all();

  $finalRoutineJSON = json_encode($routineObj);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 2023-04-09
    • 2015-04-05
    • 2016-10-03
    • 2021-05-04
    • 2019-07-17
    • 1970-01-01
    • 2019-05-23
    相关资源
    最近更新 更多