You should just be aggregating by both the first and last name columns:
$query = Student::select(DB::raw('CONCAT(first_name, last_name) AS full_name'))
->groupBy('first_name', 'last_name');
$count = $query->get()->count();
Note that your current query is invalid, because you are grouping by the full name, but are selecting other non aggregate columns. In addition, it is dangerous to assume that the concatenation of the first and last name means what you think it does. Consider:
bo derick
bode rick
Both of these people (different) would have the same concatenated full name. Instead, aggregate by the first and last name columns separately.
Edit:
I don't know how Laravel's count()
function be implemented under the hood, specifically whether or not it be smart enough to know to do the count on the MySQL server, rather than bringing the entire result set over to PHP and counting it. It might be beneficial to just do a count query directly on MySQL:
$sql = "SELECT COUNT(*) FROM (SELECT 1 FROM student GROUP BY first_name, last_name) t";
$count = Student::select(DB::raw($sql))->get();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…