前提・実現したいこと
Model内で3つのテーブルを結合させ、コントローラーに渡したいです。
可能であれば、countEvaluationsメソッド及びgetTeachersPointQueryメソッドは残した状態で、解決したいです。
発生している問題・エラーメッセージ
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'evaluations' (SQL: select `users`.*, `evaluations`.`reviews` as `reviews`, `evaluations`.`avg_point` as `evaluations_avg_point` from `users` left join (select `evaluations`.`user_teacher_id`, count('user_teacher_id') as reviews from `evaluations` where `evaluations`.`deleted_at` is null group by `evaluations`.`user_teacher_id`) as `evaluations` on `users`.`id` = `evaluations`.`user_teacher_id` left join (select `evaluations`.`user_teacher_id`, avg(evaluations.point) as avg_point from `evaluations` where `evaluations`.`deleted_at` is null group by `evaluations`.`user_teacher_id`) as `evaluations` on `users`.`id` = `evaluations`.`user_teacher_id` where `users`.`id` = 2 and `users`.`deleted_at` is null limit 1)
該当のソースコード
PHP
1public function countEvaluations() 2{ 3 // 講師別のレビュー数を取得 4 return Evaluation::query() 5 ->select( 6 'evaluations.user_teacher_id', DB::raw("count('user_teacher_id') as reviews") 7 ) 8 ->groupBy('evaluations.user_teacher_id'); 9}
PHP
1public function getTeachersPointQuery() 2{ 3 // 講師別の評価値を集計 4 return Evaluation::query() 5 ->select( 6 'evaluations.user_teacher_id', 7 DB::raw('avg(evaluations.point) as avg_point') 8 ) 9 ->groupBy('evaluations.user_teacher_id'); 10}
PHP
1public function show($id) 2{ 3 // 講師のレビュー数を取得 4 $reviews = $this->countEvaluations(); 5 // 講師の評価を取得 6 $evaluations = $this->getTeachersPointQuery(); 7 return self::query() 8 // 講師のレビュー数 9 ->leftJoinSub($reviews, 'evaluations', function ($join) { 10 $join->on('users.id', '=', 'evaluations.user_teacher_id'); 11 }) 12 // 講師の評価 13 ->leftJoinSub($evaluations, 'evaluations', function ($join) { 14 $join->on('users.id', '=', 'evaluations.user_teacher_id'); 15 }) 16 ->where('users.id', '=', $id) 17 ->select([ 18 'users.*', 19 'evaluations.reviews as reviews', 20 'evaluations.avg_point as evaluations_avg_point', 21 ]); 22}
テーブル構造
evaluations +-----------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+------------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | user_student_id | int unsigned | NO | | NULL | | | user_teacher_id | int unsigned | NO | | NULL | | | lesson_id | int unsigned | NO | | NULL | | | point | tinyint unsigned | NO | | NULL | | | comment | varchar(255) | NO | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | | deleted_at | timestamp | YES | | NULL | | +-----------------+------------------+------+-----+---------+----------------+ users +-------------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------------+------------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | email | varchar(255) | NO | UNI | NULL | | | email_verified_at | timestamp | YES | | NULL | | | password | varchar(255) | NO | | NULL | | | account | varchar(255) | NO | UNI | NULL | | | status | tinyint unsigned | YES | | NULL | | | sex | tinyint unsigned | NO | | 0 | | | age | tinyint unsigned | YES | | NULL | | | country_id | tinyint unsigned | YES | | NULL | | | prefecture_id | int unsigned | YES | | NULL | | | language_id | int unsigned | YES | | NULL | | | img | varchar(255) | YES | | NULL | | | profile | varchar(1000) | NO | | NULL | | | mailing | tinyint unsigned | NO | | NULL | | | bank_type | tinyint unsigned | YES | | NULL | | | bank_id | int unsigned | YES | | NULL | | | credit_id | int unsigned | YES | | NULL | | | category1_id | tinyint unsigned | YES | | NULL | | | category2_id | tinyint unsigned | YES | | NULL | | | category3_id | tinyint unsigned | YES | | NULL | | | category4_id | tinyint unsigned | YES | | NULL | | | category5_id | tinyint unsigned | YES | | NULL | | | withdraw_reason | varchar(255) | YES | | NULL | | | remember_token | varchar(100) | YES | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | | deleted_at | timestamp | YES | | NULL | | +-------------------+------------------+------+-----+---------+----------------+
試したこと
leftJoinにしたり、joinにしたり、試しましたが、うまくいきませんでした。
また、countEvaluations及びgetTeachersPointQueryを、1つのみ実行した場合は、問題なく実行されます。
補足情報(FW/ツールのバージョンなど)
Laravel Framework 8.14.0
PHP 7.4.9
回答1件
あなたの回答
tips
プレビュー