$topic_users = DB::table('topics')->select(DB::raw('user_id, count(*) as topic_count'))->where('created_at', '>=', Carbon::now()->subDays(60))->groupBy('user_id')->get();
或
$topic_users = DB::table('topics')->selectRaw('user_id, count(*) as topic_count')->where('created_at', '>=', Carbon::now()->subDays(60))->groupBy('user_id')->get();
但执行以下代码:
$topic_users = Topic::select(DB::raw('user_id, count(*) as topic_count'))->where('created_at', '>=', Carbon::now()->subDays(60))->groupBy('user_id')->get();
或
$topic_users = Topic::query()->select(DB::raw('user_id, count(*) as topic_count'))->where('created_at', '>=', Carbon::now()->subDays(60))->groupBy('user_id')->get();
或
$topic_users = Topic::selectRaw('user_id, count(*) as topic_count')->where('created_at', '>=', Carbon::now()->subDays(60))->groupBy('user_id')->get();
报错:
Illuminate\Database\Eloquent\MissingAttributeException The attribute [view_count] either does not exist or was not retrieved for model [App\Models\Topic].
<table>
<tbody>
<!-- First row -->
<tr>
<!-- Cells in the first row -->
<td></td>
<td></td>
<td></td>
</tr>
<!-- Second row -->
<tr>
<!-- Cells in the second row -->
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
Untracked files:
(use "git add <file>..." to include in what will be committed)
"m/\345\210\244\346\226\255\347\224\250\346\210\267\345\256\242\346\210\267\347\253\257\346\265\217\350\247\210\345\231\250\350\256\276\345\244\207\347\232\204\347\261\273\345\236\213/"
"m/\350\247\206\345\217\243/"