英文:
Convert row SQL query to Laravel Eloquent Query
问题
我需要这个查询成为Laravel的Eloquent查询,并且,如何将上面的表格在一个单一的Eloquent关联查询中连接起来?
我已经尝试了许多示例来解决这个查询,如果有解决方法,请在回复中描述给我。链接说明在此
英文:
I need this query to be Laravel eloquent, and also, how can I join the above tables in one single eloquent relational query?
I have tried many examples to solve this query if any ideas on how to solve this please describe me in response.enter link description here
答案1
得分: 2
这是源代码,其中包含一些假设。由于很难假设所有相关的表格和其内容。
php artisan make:model Course
class Course extends Model
{
protected $table = 'courses';
public function subscriptions()
{
return $this->belongsToMany(Subscription::class);
}
public function lessons()
{
return $this->hasMany(Lesson::class);
}
}
更新用户模型:
php artisan make:model Subscription
class Subscription extends Model
{
protected $table = 'subscriptions';
public function client()
{
return $this->belongsTo(Client::class);
}
public function courses()
{
return $this->belongsToMany(Course::class);
}
}
php artisan make:model Client
class Client extends Model
{
protected $table = 'clients';
public function subscriptions()
{
return $this->hasMany(Subscription::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
class Lesson extends Model
{
protected $table = 'lessons';
public function courses()
{
return $this->belongsToMany(Course::class);
}
public function users()
{
return $this->belongsToMany(User::class)->withPivot('result');
}
}
web.php:
Route::get('/view-course/{client_id}', ['as' => 'course.view', 'uses' => 'CourseController@viewCourseByClient']);
这是在CourseController控制器中编写的最终查询:
public function viewCourseByClient($client_id){
$completed_courses = Course::select('courses.name as course_name', 'courses.description as course_description')
->addSelect(DB::raw('SUM(course_completed) as user_completed_count'))
->addSelect(DB::raw('COUNT(DISTINCT tmp.user_id) as total_user_count'))
->join('subscription_courses', 'courses.id', '=', 'subscription_courses.course_id')
->join('subscriptions', 'subscription_courses.subscription_id', '=', 'subscriptions.id')
->join('clients', 'subscriptions.client_id', '=', 'clients.id')
->join('users', 'clients.id', '=', 'users.client_id')
->join('course_lessons', 'courses.id', '=', 'course_lessons.course_id')
->join('lessons', 'course_lessons.lesson_id', '=', 'lessons.id')
->leftJoin('user_lessons', function ($join) use ($client_id) {
$join->on('lessons.id', '=', 'user_lessons.lesson_id')
->on('users.id', '=', 'user_lessons.user_id')
->where('user_lessons.result', '=', 'completed')
->where('clients.id', '=', $client_id);
})
->groupBy('courses.id', 'users.id')
->selectSub(function ($query) {
$query->selectRaw('CASE WHEN COUNT(DISTINCT CASE WHEN user_lessons.result = \'completed\' THEN course_lessons.lesson_id END) = COUNT(DISTINCT course_lessons.lesson_id) THEN 1 ELSE 0 END as course_completed')
->from('courses')
->join('subscription_courses', 'courses.id', '=', 'subscription_courses.course_id')
->join('subscriptions', 'subscription_courses.subscription_id', '=', 'subscriptions.id')
->join('clients', 'subscriptions.client_id', '=', 'clients.id')
->join('users', 'clients.id', '=', 'users.client_id');
})
return view('course.completed_courses', compact($completed_courses));
}
希望这能帮助您。
英文:
Here was the source code which builds on its own with some assumptions. Because it was difficult to assume all related tables and it's content.
php artisan make:model Course
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
class Course extends Model
{
protected $table = 'courses';
public function subscriptions()
{
return $this->belongsToMany(Subscription::class);
}
public function lessons()
{
return $this->hasMany(Lesson::class);
}
}
<!-- end snippet -->
Update User Model
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
class User extends Model
{
protected $table = 'users';
public function client()
{
return $this->belongsTo(Client::class);
}
public function lessons()
{
return $this->belongsToMany(Lesson::class)->withPivot('result');
}
}
<!-- end snippet -->
php artisan make:model Subscription
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
class Subscription extends Model
{
protected $table = 'subscriptions';
public function client()
{
return $this->belongsTo(Client::class);
}
public function courses()
{
return $this->belongsToMany(Course::class);
}
}
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
class Client extends Model
{
protected $table = 'clients';
public function subscriptions()
{
return $this->hasMany(Subscription::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
<!-- end snippet -->
php artisan make:model Client
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
class Lesson extends Model
{
protected $table = 'lessons';
public function courses()
{
return $this->belongsToMany(Course::class);
}
public function users()
{
return $this->belongsToMany(User::class)->withPivot('result');
}
}
<!-- end snippet -->
web.php
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
Route::get('/view-course/{client_id}', ['as' => 'course.view', 'uses' => 'CourseController@viewCourseByClient']);
<!-- end snippet -->
Here is the final Query which you write in CourseController controller.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
public function viewCourseByClient($client_id){
$completed_courses = Course::select('courses.name as course_name', 'courses.description as course_description')
->addSelect(DB::raw('SUM(course_completed) as user_completed_count'))
->addSelect(DB::raw('COUNT(DISTINCT tmp.user_id) as total_user_count'))
->join('subscription_courses', 'courses.id', '=', 'subscription_courses.course_id')
->join('subscriptions', 'subscription_courses.subscription_id', '=', 'subscriptions.id')
->join('clients', 'subscriptions.client_id', '=', 'clients.id')
->join('users', 'clients.id', '=', 'users.client_id')
->join('course_lessons', 'courses.id', '=', 'course_lessons.course_id')
->join('lessons', 'course_lessons.lesson_id', '=', 'lessons.id')
->leftJoin('user_lessons', function ($join) use ($client_id) {
$join->on('lessons.id', '=', 'user_lessons.lesson_id')
->on('users.id', '=', 'user_lessons.user_id')
->where('user_lessons.result', '=', 'completed')
->where('clients.id', '=', $client_id);
})
->groupBy('courses.id', 'users.id')
->selectSub(function ($query) {
$query->selectRaw('CASE WHEN COUNT(DISTINCT CASE WHEN user_lessons.result = \'completed\' THEN course_lessons.lesson_id END) = COUNT(DISTINCT course_lessons.lesson_id) THEN 1 ELSE 0 END as course_completed')
->from('courses')
->join('subscription_courses', 'courses.id', '=', 'subscription_courses.course_id')
->join('subscriptions', 'subscription_courses.subscription_id', '=', 'subscriptions.id')
->join('clients', 'subscriptions.client_id', '=', 'clients.id')
->join('users', 'clients.id', '=', 'users.client_id');
return view('course.completed_courses', compact($completed_courses));
}
<!-- end snippet -->
Hope this will help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论