将SQL查询转换为Laravel Eloquent查询。

huangapple go评论64阅读模式
英文:

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 = &#39;courses&#39;;

    public function subscriptions()
    {
        return $this-&gt;belongsToMany(Subscription::class);
    }

    public function lessons()
    {
        return $this-&gt;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 = &#39;users&#39;;

    public function client()
    {
        return $this-&gt;belongsTo(Client::class);
    }

    public function lessons()
    {
        return $this-&gt;belongsToMany(Lesson::class)-&gt;withPivot(&#39;result&#39;);
    }
}

<!-- 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 = &#39;subscriptions&#39;;

    public function client()
    {
        return $this-&gt;belongsTo(Client::class);
    }

    public function courses()
    {
        return $this-&gt;belongsToMany(Course::class);
    }
}

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

class Client extends Model
{
    protected $table = &#39;clients&#39;;

    public function subscriptions()
    {
        return $this-&gt;hasMany(Subscription::class);
    }

    public function users()
    {
        return $this-&gt;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 = &#39;lessons&#39;;

    public function courses()
    {
        return $this-&gt;belongsToMany(Course::class);
    }

    public function users()
    {
        return $this-&gt;belongsToMany(User::class)-&gt;withPivot(&#39;result&#39;);
    }
}

<!-- end snippet -->

web.php

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Route::get(&#39;/view-course/{client_id}&#39;, [&#39;as&#39; =&gt; &#39;course.view&#39;, &#39;uses&#39; =&gt; &#39;CourseController@viewCourseByClient&#39;]);

<!-- 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(&#39;courses.name as course_name&#39;, &#39;courses.description as course_description&#39;)
    -&gt;addSelect(DB::raw(&#39;SUM(course_completed) as user_completed_count&#39;))
    -&gt;addSelect(DB::raw(&#39;COUNT(DISTINCT tmp.user_id) as total_user_count&#39;))
    -&gt;join(&#39;subscription_courses&#39;, &#39;courses.id&#39;, &#39;=&#39;, &#39;subscription_courses.course_id&#39;)
    -&gt;join(&#39;subscriptions&#39;, &#39;subscription_courses.subscription_id&#39;, &#39;=&#39;, &#39;subscriptions.id&#39;)
    -&gt;join(&#39;clients&#39;, &#39;subscriptions.client_id&#39;, &#39;=&#39;, &#39;clients.id&#39;)
    -&gt;join(&#39;users&#39;, &#39;clients.id&#39;, &#39;=&#39;, &#39;users.client_id&#39;)
    -&gt;join(&#39;course_lessons&#39;, &#39;courses.id&#39;, &#39;=&#39;, &#39;course_lessons.course_id&#39;)
    -&gt;join(&#39;lessons&#39;, &#39;course_lessons.lesson_id&#39;, &#39;=&#39;, &#39;lessons.id&#39;)
    -&gt;leftJoin(&#39;user_lessons&#39;, function ($join) use ($client_id) {
        $join-&gt;on(&#39;lessons.id&#39;, &#39;=&#39;, &#39;user_lessons.lesson_id&#39;)
            -&gt;on(&#39;users.id&#39;, &#39;=&#39;, &#39;user_lessons.user_id&#39;)
            -&gt;where(&#39;user_lessons.result&#39;, &#39;=&#39;, &#39;completed&#39;)
            -&gt;where(&#39;clients.id&#39;, &#39;=&#39;, $client_id);
    })
    -&gt;groupBy(&#39;courses.id&#39;, &#39;users.id&#39;)
    -&gt;selectSub(function ($query) {
        $query-&gt;selectRaw(&#39;CASE WHEN COUNT(DISTINCT CASE WHEN user_lessons.result = \&#39;completed\&#39; THEN course_lessons.lesson_id END) = COUNT(DISTINCT course_lessons.lesson_id) THEN 1 ELSE 0 END as course_completed&#39;)
            -&gt;from(&#39;courses&#39;)
            -&gt;join(&#39;subscription_courses&#39;, &#39;courses.id&#39;, &#39;=&#39;, &#39;subscription_courses.course_id&#39;)
            -&gt;join(&#39;subscriptions&#39;, &#39;subscription_courses.subscription_id&#39;, &#39;=&#39;, &#39;subscriptions.id&#39;)
            -&gt;join(&#39;clients&#39;, &#39;subscriptions.client_id&#39;, &#39;=&#39;, &#39;clients.id&#39;)
            -&gt;join(&#39;users&#39;, &#39;clients.id&#39;, &#39;=&#39;, &#39;users.client_id&#39;);

  return view(&#39;course.completed_courses&#39;, compact($completed_courses));
}

<!-- end snippet -->

Hope this will help you.

huangapple
  • 本文由 发表于 2023年4月10日 19:46:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定