mercredi 6 septembre 2017

Laravel 5 - Grouping, Counting and Outputting eager loaded relationships

Pretty new to Laravel and am trying to build a web app to help me run my Martial Arts business. Thanks so much for the help!

Overview:

The webapp allows me to create Lessons which I can add Students and Techniques to. Each Technique can have multiple Variations of the technique but not all Variations have to be added to a lesson.

Once those are added, I want to display all of the Lessons/Techniques/Variations on one page for the student.

Controllers:

HomeController.php:

public function studentProfile($id)
{


    // Get Student and include all Lessons, Techniques and Variations
    $students = Student::with(['lesson', 'lesson.technique', 'lesson.technique.variation'])->where('id',$id)->get();

    return view('techniques', compact('students')); 
}

View:

technique.blade.php

@extends('layouts.dashboard.dashboard')

@section('content')
<main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main">

@foreach ($students as $student)
  <h1></h1>
@endforeach 





 <h2>Fundamentals</h2>
      <div class="table-responsive">
        <table class="table table-striped">
          <thead>
            <tr>
              <th>Mount</th>
              <th>#</th>
              <th>Guard</th>
              <th>#</th>
              <th>Side Mount</th>
              <th>#</th>
              <th>Standing</th>
              <th>#</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td><strong>1. Trap and Roll</strong><br>- Standard<br>- Headlock<br>- Punch Block</td>
              <td><br>3<br>4<br>3</td>
              <td><strong>1. Punch Block Stage 1-4</strong><br>- Stage 1<br>- Stage 2<br>- Stage 3<br>- Stage 4</td>
              <td><br>1<br>1<br>1<br>1</td>
              <td><strong>1. Positional Control</strong><br>- Roll Prevention<br>- Guard Prevention<br>- Mount Transition</td>
              <td><br>1<br>1<br>1</td>
              <td><strong>1. Clinch</strong><br>- Aggressive<br>- Conservative</td>
              <td><br>1<br>1</td>
            </tr>

          </tbody>
        </table>
      </div>
     </main>
@endsection

Models:

Techniques (belongsToMany::Lesson, hasMany::Variation):

  • id,
  • name,
  • category,
  • subcategory,
  • level,
  • timestamps

Variations (belongsTo::Technique, belongsTo::Lesson):

  • id,
  • name,
  • technique_id,
  • timestamps

Students (belongsTo::User, belongsToMany::Lesson):

  • id,
  • name,
  • birthdate,
  • user_id,
  • timestamps

Lessons (belongsToMany::Technique, belongsToMany::Student, belongsToMany::Variation):

  • id,
  • name,
  • status,
  • timestamps

Each has the correct pivot table (eg: lesson_variations).

I can dd($students) and see all the nested relationships but I am having a very hard time understanding how I can groupBy and count the results. I want to groupBy multiple ways including:

  • I want to Group all the Techniques by Level (Fundamentals, Advanced).

  • Then I want to display all the Techniques by Category and then SubCategory and finally,

  • I want to count all the variations that relate to the technique so if there are 10 lessons and in those 10 lessons, you did Technique trap and roll 8 times and the variation Standard 6 times it would show:

Fundamentals:

Category

Sub Category

Technique

Technique's Variations Count over all lessons.

What I want to accomplish

Thank you!

Thank you so much for your help! I really appreciate it! Once this is done the webapp should be finished it's first version and will help save me hours of work per week!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire