复杂查询有问题 (Laravel Eloquent)

Having problems with complex query (Laravel Eloquent)

久违了:D

最近,我一直在尝试开发一个网站,但似乎没有得到解决方案。已经 3 周了,我还做不到。 我没有使用模型,因为我仍然没有像我想的那样理解它们,也许 hasmany 或 belongsto 之类的东西会有所作为,但我不知道如何使用它们


这是查询:

$threads = DB::table('threads')
    ->join('users as author', 'author.id', '=', 'threads.user_id')
    ->join('replies', 'replies.thread_id', '=', 'threads.id')
    ->join('categories', 'categories.id', '=', 'threads.category_id')
    ->join('users as users', 'users.id', '=', 'replies.user_id')
    ->select('threads.id as thread_id','threads.category_id as thread_category','threads.title as thread_title', 'replies.body as thread_first_message', 'author.name as thread_author', 'author.id as author_id', 'replies.created_at as last_reply_date', 'users.name as last_reply_user', 'users.id as lru_id', 'threads.visits', 'threads.hd', 'threads.18', 'threads.prv', DB::raw('(SELECT COUNT(id) FROM replies WHERE thread_id = threads.id) as count_replies'))
    ->where('replies.created_at', '=', DB::raw('(SELECT max(created_at) FROM replies)'));

我需要从 3 tables 中获取多个数据。

我将解释每篇专栏的内容:

thread_id: 字段 (id) 来自 table 'threads'

thread_category:来自 'threads' 的字段 (category_id) 和来自 table 'categories' 的外键 ( id)

thread_title:字段(标题)来自 'threads'

thread_first_message第一个字段(body)来自table'replies' 其中 thread_id = (thread_id)

thread_author:字段 (user_id) 来自 'threads' 和外键来自 table 'users' ( id)

author_id:'users' 中写第一条消息的字段 (id)

last_reply_date:来自 'replies' 的字段 (created_at) 其中 'replies.id' = (thread_id)

last_reply_users:来自 'users' 的字段(名称)在 (thread_id)

lru_id:来自 'users' 的字段 (id) 来自 'users',在 (thread_id)

visits:字段(访问)来自 table 'threads'

hd, prv, 18:布尔字段(它们在这里不重要,也许在将来)来自 table 'threads'

count_replies:来自 thread_id = (thread_id 的所有回复的总和)


数据库目前看起来像这样:


我的问题是我在数据库中有 2 条记录,所以页面中应该显示 2 个 div,但只显示一个:

我得到的结果:

我需要的结果:

因为线程 table 有 2 条记录:

你们能帮帮我吗?提前谢谢你

编辑:我到这里为止:

SELECT DISTINCT(t.id), t.category_id, t.title, r1.body, u1.name as author, u1.id, r2.created_at, u2.id as last_author_id, u2.name as last_author, t.visits, COUNT(DISTINCT(r3.id)) from threads t, replies r1, replies r2, replies r3, users u1, users u2 where r1.thread_id = t.id and u1.id = t.user_id and r2.thread_id = t.id and r2.user_id = u2.id and r3.thread_id = t.id group by t.id

它检索这个:

我不知道如何将其转换为 Eloquent 查询或者它是否是最优的

让我们按照 Laravel 的方式进行。

首先,您应该创建模型。创建目录 app/Models 并将这些文件放在那里:

Thread.php:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Thread extends Model {

  public function author() {
    return $this->belongsTo(User::class, 'user_id')
  }

  public function category() {
    return $this->belongsTo(Category::class, 'category_id')
  }

  public function replies() {
    return $this->hasMany(Reply::class, 'thread_id');
  }
}

User.php:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class User extends Model {

  public function threads() {
    return $this->hasMany(Thread::class, 'user_id')
  }

  public function replies() {
    return $this->hasMany(Reply::class, 'user_id')
  }

  public function votes() {
    return $this->hasMany(Vote::class, 'user_id')
  }
}

Reply.php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Reply extends Model {

  public function thread() {
    return $this->belongsTo(Thread::class, 'thread_id')
  }

  public function author() {
    return $this->belongsTo(User::class, 'user_id')
  }

  public function votes() {
    return $this->hasMany(Vote::class, 'reply_id')
  }
}

Category.php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Category extends Model {

  public function threads() {
    return $this->hasMany(Thread::class, 'category_id')
  }
}

Vote.php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Vote extends Model {

  public function author() {
    return $this->belogsTo(User::class, 'user_id')
  }

  public function reply() {
    return $this->belogsTo(Reply::class, 'reply_id')
  }
}

现在,给定一个Thread,你可以轻松得到你需要的结果

// last 10 threads
$threads = Thread::orderBy('created_at', 'desc')
   ->with('category')->limit(10)->get();

// You can loop on the blade template and display the informations you need
foreach($threads as $thread) {
  $thread->category->id; // The thread category ID
  $thread->category->name; // The thread category name

  $lastReply = $thread->replies()->orderBy('created_at', 'desc')->first(); // The last reply on the thread
  $lastReply->created_at; // Date of the latest reply
  $lastReply->author->name; // Name of the user that created the last reply

  $thread->visits; // The number of visits on the thread
  $repliesCount = $thread->replies()->count(); // Number of replies on the thread
}