$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].