Hello!
I have three table and one linking table
programs table
id | name
1 | for mass
2 | for strength
exercises table
id | name
1 | arms-exercise-1
2 | arms-exercise-2
3 | arms-exercise-3
4 | arms-exercise-4
5 | legs-exercise-1
6 | legs-exercise-2
7 | legs-exercise-3
8 | legs-exercise-4
timetables
id | week | day_of_week
1 | odd | 1
2 | odd | 2
3 | odd | 3
4 | odd | 4
5 | odd | 5
6 | odd | 6
7 | odd | 7
8 | even | 1
9 | even | 2
10 | even | 3
11 | even | 4
12 | even | 5
13 | even | 6
14 | even | 7
exercise_program_timetable linking table
id | program_id | exercise_id | timetable_id
1 | 1→ | 1→ | 1→
2 | 1→ | 2→ | 2→
3 | 1→ | 1→ | 3→
4 | 1→ | 3→ | 4→
6 | 1→ | 1→ | 5→
7 | 1→ | 4→ | 6→
8 | 1→ | 5→ | 7→
And relationships in models
class Program extends Model
{
public function Exercises()
{
return $this->belongsToMany(Exercise::class, 'exercise_program_timetable');
}
public function Timetables()
{
return $this->belongsToMany(Timetable::class, 'exercise_program_timetable');
}
}
class Exercise extends Model
{
public function Programs()
{
return $this->belongsToMany(Program::class, 'exercise_program_timetable');
}
public function Timetables()
{
return $this->belongsToMany(Timetable::class, 'exercise_program_timetable');
}
}
class Timetable extends Model
{
public function Programs()
{
return $this->belongsToMany(Program::class, 'exercise_program_timetable');
}
public function Exercises()
{
return $this->belongsToMany(Exercise::class, 'exercise_program_timetable');
}
}
My questions:
Firstly, without specifying the linking table
... ::class, 'exercise_program_timetable');
Laravel says I can't find the table...
Is this how it should be?
I need to show a schedule of program "for mass" , for example from today and 10 days in advance, based on table "timetables"
in controller
public function show(Program $program)
{
$program->load('exercise.timetabless')->get();
return response()->view('programs.show', ['program' => $program]);
}
and trash in view ))
<h4>{{ $program->name }}</h4>
<h5>Odd week</h5>
@for ($i = 1; $i <= 7; $i++)
<ul>
<li><h6>Day of week: {{ $i }}</h6>
<ul>
@foreach ($program->menus as $menu)
@if ( $menu->week === 'odd' && $menu->day_of_week === $i )
@foreach ($menu->dishes as $dish)
<li>{{ $dish->name }}</li>
@endforeach
@endif
@endforeach
</ul>
</li>
</ul>
@endfor
Firstly, exercises for the day are shown several times (according to the number of entries in the linking table)
Well, and secondly, is it basically wrong?
How can I get only the data I need so that the logic is in the model and so that such a list can be built
Name of program
Odd week
Day_of_week(1)
Exercise_1
...
Exercise_N
Day_of_week(2)
Exercise_1
...
Exercise_N
...
...
...
Day_of_week(7)
Exercise_1
...
Exercise_N