【问题标题】:how to order entries by likes of user_id?如何按 user_id 之类的条目排序?
【发布时间】:2019-07-04 14:08:44
【问题描述】:

我正在开发一些 laravel 应用程序,并且我有一个名为“条目”的表格,我在此表格中保存了比赛名称的建议。我需要根据用户总共有多少喜欢的条目对条目进行排序。

这是我的桌子:

id      user_id     contest_id     name               liked

1       1           1              test.com           true
2       4           1              cool.com           false
3       1           1              code.com           true
4       3           1              tool123.com        false
5       2           1              a23423.com         true
6       3           1              dole.net           true
7       1           1              great.com          false
8       2           1              domain.com         true
9       2           1              gol.com            false
10      2           1              greatcode.com      true
11      2           2              greatco.com        true
12      2           2              greatmap.com       true

这就是我所拥有的:

$entries = ContestEntry::where('contest_id', '=', $contest->id)
->orderBy('liked', 'desc')
->orderBy('created_at', 'desc')
->get();

我需要通过以下方式订购条目:

给定的 $contest->id(第一行) 是否喜欢该条目(第 2 行) 用户有更多喜欢的条目(全球范围内,而不是当前比赛) 并通过 created_at(第 3 行)

架构:

Schema::create('contest_entries', function (Blueprint $table) { 
  $table->increments('id'); 
  $table->integer('user_id');
  $table->integer('contest_id');
  $table->text('name');
  $table->text('comment')->nullable();
  $table->boolean('liked')->default(false);
  $table->boolean('available')->default(true);
  $table->timestamp('checked_at');
  $table->timestamps(); 
});

“显示创建表竞赛条目”的输出:

CREATE TABLE `contest_entries` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` int(11) NOT NULL,
 `contest_id` int(11) NOT NULL,
 `name` text COLLATE utf8mb4_unicode_ci NOT NULL,
 `comment` text COLLATE utf8mb4_unicode_ci,
 `liked` tinyint(1) NOT NULL DEFAULT '0',
 `available` tinyint(1) NOT NULL DEFAULT '1',
 `checked_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 `created_at` timestamp NULL DEFAULT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

我需要的是如下表:

id      user_id     contest_id     name               liked

5       2           1              a23423.com         true
8       2           1              domain.com         true
9       2           1              gol.com            false
10      2           1              greatcode.com      true
1       1           1              test.com           true
3       1           1              code.com           true
7       1           1              great.com          false
4       3           1              tool123.com        false
6       3           1              dole.net           true
2       4           1              cool.com           false

如您所见,它仅显示来自比赛 ID 1 的数据,用户 ID 2 在所有比赛中都有 5 个喜欢 = true 这就是为什么它的条目是第一个,用户 ID 1 有 2 个喜欢 = true 这就是为什么它的条目是第二个......然后继续...

【问题讨论】:

  • 你的表架构是什么?
  • 请添加show create table contest_entries的sql输出。
  • 您似乎没有created_at 字段。那么你希望如何订购呢?
  • laravel 在添加 $table->timestamps() 时添加 created_at 字段;在架构中。
  • "user_id 2 has 5 like = true" -- 我只看到 3 个?? (ID 5,8,10)。请澄清。

标签: laravel eloquent


【解决方案1】:

这将有助于在特定比赛中按赞对用户列表进行排序。

$entries = DB::table('contest_entries')
     ->where('contest_id', '=', $contest->id)
     ->where('liked',true)
     ->select('user_id', DB::raw('count(*) as total'))
     ->groupBy('user_id')
     ->orderByDesc('total')
     ->get();

【讨论】:

  • 该查询出现此错误:函数 Illuminate\Support\Collection::get() 的参数太少,在 /var/www/html/app/Http/Controllers/ 中传递了 0第 66 行的 AccountController.php 和至少 1 个预期
  • 感谢您的回答。但这不是我需要的。我需要的是同一张表,其中包含所有条目及其由 users_id 排序的所需竞赛 ID 的数据(在同一张表中全局更喜欢 = true)
  • Collection {#281 ▼ #items: array:1 [▼ 0 => {#277 ▼ +"user_id": 2 +"total": 3 } ] }
  • 我的意思是,您期望的结果(包含所有条目及其数据的同一张表......)。
  • 我刚刚得到它。实际上你有代码的订单。现在您必须将原始数据与此结果保持连接,以获得您期望的结果。
【解决方案2】:

经过大量研究,最后我在 ContestEntry 模型中创建了一个新范围,并将 2 个查询组合如下:

public function scopeOrderByLikes($query){

    $order = ContestEntry::select('user_id', \DB::raw('count(*) as total'))
    ->groupBy('user_id')
    ->orderByDesc('total')
    ->pluck('user_id');

    return $query->orderBy('user_id',$order);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2015-08-11
    相关资源
    最近更新 更多