I am trying to query a pivot table to show how many exercises have one day, but there is something wrong with that.
I need to get all the exercises by Monday or any day.
I have three tables: routines
, exercises
and exercise_routine
.
routines exercise_routine exercise
-------- ---------------- --------
id id id
name routine_id name
description exercise_id description
user_id week_day_id
sets
reps
I would like to get all the exercises by week_day_id
, hope you understand my problem.
I tried these examples from other people on stackoverflow but it does not work.
Exercise::whereHas('routines', function($q) {
$q->where('routines.week_day_id', 1);
})
->get();
return \DB::table('exercises')
->join('exercise_routine', 'exercise_routine.exercise_id', '=', 'exercises.id')
->where('exercise_routine', 1)
->get();
dd( DB::table('exercises')
->where('exercises.id', '=', 1)
->select('exercises.id'));
// Routine Model
public function exercises() {
return $this->belongsToMany('App\Models\Exercise');
}
// ExerciseModel
public function routines() {
return $this->belongsToMany('App\Models\Routine')->as('plan')->withPivot('sets', 'reps', 'week_day_id')->withTimestamps();
}
// Controller
public function show(WeekDay $weekday) {
Exercise::whereHas('routines', function($q, $weekday) {
$q->where('routines.week_day_id', $weekday);
})
->get();
}
// api routes
Route::group(['prefix' => '/{weekday}/exercises'], function () {
Route::get('/', 'WeekDayExerciseController@show')->middleware('auth:api');
});
I expected to get all the exercises by Monday for example like this:
{
"id": 236,
"name": "Upright Row (Barbell)",
"description": "Description for Upright Row (Barbell)"
},
{
"id": 237,
"name": "Upright Row (Cable)",
"description": "Description for Upright Row (Cable)"
},
{
"id": 238,
"name": "Upright Row (Dumbbell)",
"description": "Description for Upright Row (Dumbbell)"
},
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ZvZvjO
via IFTTT
Aucun commentaire:
Enregistrer un commentaire