Add to Model: public static function getTableName() { return (new self())->getTable(); } Use: $result = DB::select('SELECT COUNT(*) as num FROM (SELECT visitor_id FROM ' . Visitor::getTableName() . ' WHERE user_id=? AND deleted_at IS NULL GROUP BY visitor_id) AS t_temp', [$targetUser->id]); if (is_array($result) && count($result) > 0) { $visitorNew = $result[0]->num; } else { Log::error('count new visitor count error.'); return response()->json(['code' => '500', 'msg' => 'error'], 500)->send(); }